Destructive functions?

Started by hsmyers, April 14, 2008, 02:08:25 PM

Previous topic - Next topic

hsmyers

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
\"Censeo Toto nos in Kansa esse decisse.\"—D. Gale \"[size=117]ℑ♥λ[/size]\"—Toto

cormullion

#1
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...

Jeff

#2
You don't need a macro for that; just pass a quoted symbol.
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code

hsmyers

#3
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
\"Censeo Toto nos in Kansa esse decisse.\"—D. Gale \"[size=117]ℑ♥λ[/size]\"—Toto