Another Lisp beginner here. I'm currently reading ANSI Common Lisp by Paul Graham (PG). I've come to the chapter on functions and the sub chapter on closures. There are some things that confuse me. *Thought about it for a while.* Hm, now that I have thought about it I think that I have pretty much worked out most of what confused me. But I'm probably wrong about some things so I'll just write some thoughts and you can point out to me where I'm wrong. There are some questions in there, too.
- Code: Select all
(defun make-adder (n)
(+ x n)))
But apparently it works just as well without the #'. What's the difference? Why do PG write #' if it isn't needed? I thought #' meant something like "Get the function that this symbol refers to, from the function namespace (or whatever it's called)." But (lambda ...) isn't a symbol, so I shouldn't need it, right? But then, why does it even work to prefix (lambda ...) with #'? I mean, if it doesn't make sense I should get an error.
Another thought I had was that it would be neat if I could do ((make-adder 3) 2) and get 5 (almost wrote 6, that would be neat). But apparently I can't. I don't understand exactly why, although I know that forms (I'm not sure I even know what a "form" is exactly) are evaluated only (?) when they are arguments to functions. So, maybe (make-adder 3) is not evaluated when I write ((make-adder 3) 2) because it's not an argument to a function. Then I tried (setf a (make-adder 3)) and (a 3) but that didn't work. I guess that's because a is now a variable in the namespace of, hm, regular values, and not in the function namespace (What are they usually called?), right? (setf (symbol-function 'a) (make-adder 3)) and then (a 3) worked. Of course, I know about (funcall (make-adder 3) 3) so I don't need to use symbol-function all the time.
So I guess there were only two proper questions in there: Why PG writes #'(lambda ...) and why ((make-adder 3) 6) doesn't work. Although, if you notice that I've got something wrong, or know of any pits I might fall into, please point that out.