[toronto-lisp] Lambda question

Brian Connoy BConnoy at morrisonhershfield.com
Tue Oct 19 18:16:59 UTC 2010

This is a trifling example (and not a meaningful illustration of CAPI), but illustrates one of my most common uses of Lambdas;  namely while developing CAPI applications in LispWorks.

If I'm uncertain how particular callbacks will behave, a simple alert will get me pointed in the right direction:

(defmacro alert (msg &rest args)
  "A tiny convenience macro for CAPI message box display."
  `(capi:display-message ,msg , at args))

(defclass my-list (capi:list-panel)
   :selection-callback  #'(lambda (&rest args)(alert "Select"))
   :retract-callback       #'(lambda (&rest args)(alert "Retract"))
   :interaction :multiple-selection
   :items '(:red :blue :green :yellow :orange :purple :pink :brown :tan)
   :visible-min-height 100
   :width 50))

(defun test ()
  (capi:contain (make-instance 'my-list)))

;; Test by evaluating:  (test)

Granted, this does not describe use of Lambdas in delivered applications, although the example callbacks above could just as easily persist as anonymous functions in the delivered CAPI application if they seem small enough to warrant it.


From: Dave Penton [mailto:djp at arqux.com]
Sent: Monday, October 18, 2010 10:42 PM
To: Aleksandar Matijaca
Cc: toronto-lisp at common-lisp.net
Subject: Re: [toronto-lisp] Lambda question

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<mailto:toronto-lisp at common-lisp.net>

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/20101019/94693239/attachment.html>

More information about the toronto-lisp mailing list