Rcjp's Weblog

January 23, 2007

Symbol-function

Filed under: lisp — rcjp @ 9:36 am

You can make an adder function that changes its closure variable via an optional argument

    (defun make-variable-adder (n)
      (lambda (x &optional change)
        (if change
            (setq n x)
            (+ x n))))

    (setq add2 (make-variable-adder 2))
    (mapcar add2 '(1 2 3)) => (3 4 5)

But then whilst trying to change the closure variable I forgot that (add23 t) won’t work, you need (funcall add2 3 t) since (symbol-function 'add2) hasn’t been defined, only the symbol-value has. The only reason mapcar works is that as the hyperspec says… “If function is a symbol, it is coerced to a function as if by symbol-function.”

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: