getting newlisp to "wind" up? and practice sets

Started by anemo, January 10, 2011, 09:52:44 AM

Previous topic - Next topic

anemo

How do I get a function to keep what it returns as it's "referance" instead of it's value?   Or some other logical method that lets you perform getting/setting without having to write lots of messy functions?



I'm also curious if there are anything around like practice sets.   That give you a problem then also show an example.   This tends to be the fastest way for me to learn since every language has little short cuts that aren't obvious till you see it your way and then someone else's.



(set 'player-states '(
(myself (levels ( (body 100) (focus 100) (will 100))) (status '()) )
(target (levels ( (body 100) (focus 100) (will 100))) (status '()) )
))

(define
(body-level target)
(lookup 'body (lookup '(target levels) player-states)) )

;works and sets body to 110
(setf
(lookup 'body (lookup '(target levels) player-states))
110)

(body-level myself)

;does not set body to 200, how do I get the 'referance' to wind up?
(setf (body-level 'myself) 200)

(body-level myself)

Lutz

#1
Before explaining how to pass into a user defined function by reference, let me re-define 'player-states' by making it strictly a list of nested associations:


(set 'player-states '(
            (myself (levels (body 100) (focus 100) (will 100)) (status ()) )
            (target (levels (body 100) (focus 100) (will 100)) (status ()) )
))

(setf (lookup '(myself levels body) player-states) 111)
(setf (lookup '(target levels focus) player-states) 222)

(assoc 'myself player-states)
(assoc 'target player-states)

;=> (myself (levels (body 111) (focus 100) (will 100)) (status ()))
;=> (target (levels (body 100) (focus 222) (will 100)) (status ()))

; or you could do more specific
(assoc '(myself levels) player-states)
(assoc '(target levels) player-states)

;=> (levels (body 111) (focus 100) (will 100))
;=> (levels (body 100) (focus 222) (will 100))


now lets abstract out the the top record level and pass 'player-states' by reference:


; use a default functor to hold the database
(set 'player-states:player-states '(
            (myself (levels (body 100) (focus 100) (will 100)) (status ()) )
            (target (levels (body 100) (focus 100) (will 100)) (status ()) )
))

(define (modify-myself sublevel part states value)
(setf (lookup (list 'myself sublevel part) states) value))

; player-states passed by reference
(modify-myself 'levels 'body player-states 111)

(assoc 'myself player-states)
;=> (myself (levels (body 111) (focus 100) (will 100)) (status ()))

; or you could do more specific

(assoc '(myself levels) player-states)
;=> (levels (body 111) (focus 100) (will 100))

anemo

#2
Thanks for the help,   I'll look at it in detail as soon as possible.  



I'll also have to do some more reading from the looks of it.   don't quite know what the ':' does except that it's used for controlling name spaces when using objects.   The use in the last example seems weird to me, and I'm not sure I know what a functor is(or at least by that name).



Didn't even realize I mixed up the parens on the original play-states list.   Which explains why lookup was being wierd when I tried to do it that way originally.