[toronto-lisp] Lambda question

Dave Penton djp at arqux.com
Tue Oct 19 02:42:12 UTC 2010

On 2010-10-15, at 11:39 PM, Aleksandar Matijaca wrote:

> Hi there,
> a quick question - I have a reasonably firm understanding of how lambda
> functions work, however, I cannot immediately see why you would want to
> use one, when you can write a perfectly good NAMED function instead...
> So under which circumstances would you absolutely want to use a lambda
> function and why, and under which circumstances would you absolutely
> NOT want to use a lambda function..
> Thanks, Alex.
> _______________________________________________
> toronto-lisp mailing list
> toronto-lisp at common-lisp.net
> http://common-lisp.net/cgi-bin/mailman/listinfo/toronto-lisp

My thoughts (always subject to correction by more experienced Lispers):

It's true (as several posts on this thread emphasize) that creating a function and "naming" it are two different things in CL. But I think that in order to understand the "naming" part, eventually one should come to grips with what symbols and packages are in CL. It wasn't until I got a bit of a handle on symbols and packages that the relationship of function creation to function "naming" sunk in for me.

To see what I  mean try typing the following into your REPL (the weird colours are the defaults in emacs+slime):

CL-USER> (defun x () 'foo)
CL-USER> (x)
CL-USER> (setf (symbol-function 'x) (lambda () 'bar))
#<Anonymous Function #x302000C94FFF>
CL-USER> (x)

This shows that using defun to "name" a function doesn't exactly do what naming a function in, say, C would do. The two calls to "function x" above actually invoke different function objects, although they are syntactically identical. It looks a bit like pointers to functions, but it's not really, It's about the way symbols, packages, and I guess the CL reader work.

Thus the "name" of a function in CL is a somewhat more fluid thing than in many other languages. To me this way of approaching it provides a better conceptual framework than wondering "what use are anonymous functions in Lisp?" That said, unnamed functions (ones not accessible through a symbol) have lots of practical uses, as others have pointed out on this thread.

A document that helped me with all this The Complete Idiot's Guide to Common Lisp Packages by Erann Gat. Also the chapter on symbols in Graham's book ANSI Common Lisp is good. Both are floating around the Web.


- Dave -

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/toronto-lisp/attachments/20101018/c0942c4d/attachment.html>

More information about the toronto-lisp mailing list