<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On 2010-10-15, at 11:39 PM, Aleksandar Matijaca wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Hi there,<div><br></div><div>a quick question - I have a reasonably firm understanding of how lambda</div><div>functions work, however, I cannot immediately see why you would want to</div><div>use one, when you can write a perfectly good NAMED function instead...</div>
<div><br></div><div>So under which circumstances would you absolutely want to use a lambda</div><div>function and why, and under which circumstances would you absolutely</div><div>NOT want to use a lambda function..</div>
<div><br></div><div>Thanks, Alex.</div><div><br></div>
_______________________________________________<br>toronto-lisp mailing list<br><a href="mailto:toronto-lisp@common-lisp.net">toronto-lisp@common-lisp.net</a><br>http://common-lisp.net/cgi-bin/mailman/listinfo/toronto-lisp<br></blockquote></div><br><div>My thoughts (always subject to correction by more experienced Lispers):</div><div><br></div><div>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.</div><div><br></div><div>To see what I mean try typing the following into your REPL (the weird colours are the defaults in emacs+slime):</div><div><br></div><div><div><font class="Apple-style-span" color="#FA30FF">CL-USER></font> (defun x () 'foo)</div><div><font class="Apple-style-span" color="#F92712">X</font></div><div><font class="Apple-style-span" color="#FA30FF">CL-USER></font> (x)</div><div><font class="Apple-style-span" color="#F92712">FOO</font></div><div><font class="Apple-style-span" color="#FA30FF">CL-USER></font> (setf (symbol-function 'x) (lambda () 'bar))</div><div><font class="Apple-style-span" color="#F92712">#<Anonymous Function #x302000C94FFF></font></div><div><font class="Apple-style-span" color="#FA30FF">CL-USER></font> (x)</div><div><font class="Apple-style-span" color="#F92712">BAR</font></div><div><font class="Apple-style-span" color="#FA30FF">CL-USER></font> </div></div><div><br></div><div>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.</div><div><br></div><div>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.</div><div><br></div><div>A document that helped me with all this <i>The Complete Idiot's Guide to Common Lisp Packages</i> by Erann Gat. Also the chapter on symbols in Graham's book <i>ANSI Common Lisp</i> is good. Both are floating around the Web.</div><div><br></div><div>Best,</div><div><br></div><div>- Dave -</div><div><br></div></body></html>