newLISP Fan Club

Forum => Anything else we might add? => Topic started by: hsmyers on April 14, 2008, 02:08:25 PM

Title: Destructive functions?
Post by: hsmyers on April 14, 2008, 02:08:25 PM
What is the pattern for writing functions that change their parameters in the calling scope? For instance this clearly doesn't work:

(define (strip move)
  (replace "[+#]" move "" 0))

So what would?



--hsm
Title:
Post by: cormullion on April 14, 2008, 03:01:14 PM
In that example, move is local to the function, and won't survive the function.



Obviously, this is one way:


(set 'm (strip m))

cos you're usually working with the values of evaluated functions. But how about this:


(define (strip move)
  (replace "[+#]" move "" 0))

(set 'm "+blah")

(strip m)
;-> "blah"

m
;-> "+blah"

(define-macro (strip move)
  (replace "[+#]" (eval move) "" 0))

(set 'm "+blah")

(strip m)
;-> "blah"

m
;-> "blah"


Looks like we can pass the symbol itself...
Title:
Post by: Jeff on April 14, 2008, 04:46:21 PM
You don't need a macro for that; just pass a quoted symbol.
Title:
Post by: hsmyers on April 14, 2008, 05:33:25 PM
Thanks all. The use of eval means that I can use it either way, as a value for set or setq and as a direct change--- cool!



--hsm