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.
On Fri, Oct 15, 2010 at 11:39:01PM -0400, Aleksandar Matijaca wrote:
Hi there,
a quick question - I have a reasonably firm understanding of how lambda functions work
You mean just "functions", or if it's not the anonymous aspect that you're emphasizing, "closures". You wouldn't emphasize "literal strings", or "unnamed strings", would you?
, however, I cannot immediately see why you would want to use one, when you can write a perfectly good NAMED function instead...
I don't know why you would want to pass an unnamed string, e.g.:
f(s + "\n") // Java, Python (f (string-append s "\n")) ; Scheme
when you could write a perfectly good NAMED string instead:
String s1 = s + "\n" // Java, Python (drop "String") f(s1)
(let ((s1 (string-append s "\n"))) (f s1))
Once you understand that, maybe part of your question is, when would one ever pass a function? If you know any object orientation, the same question is: when would one ever pass an object having a method?
And after that, when would one ever have a function not known before running the program, i.e. produced dynamically (e.g. the way the string s + "\n" is produced dynamically). In OO: why would one make objects with state only known when running the program (and an unknown number of ojects, so they can't even all be named beforehand). Make sure you understand that lambda produces a *closure*, not just a "regular" function.
Others will presumably give examples, but the above questions let you leverage and connect your understanding of other parts of programming, to "lambda"s.
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@common-lisp.net http://common-lisp.net/cgi-bin/mailman/listinfo/toronto-lisp
The most obvious (to me :-) example is that of callbacks in gui's. It can be quite laborious to specify each callback as a separate function. In fact, the machinery behind my large gui is run by a state machine, where almost every widget/control/pane calls the same entry point (the state machine) and changes only the parameters.
Here's a snippet from that LW gui.
(new-font-button capi:button :text "Add Font" :callback-type :interface :callback #'(lambda (intf) (transition :call intf 0 0 :func 'change-font)))
The final line is the callback to the state machine.
Further, as Gary mentioned, lambda's are closures which can include (capture) references to surrounding context. For example a lambda can refer to a variable or a slot defined in its parent's scope. When the lisp compiler compiles the lambda, it compiles-in a reference to that variable. The compiled lambda can then be passed as an object (a function object) to any other place and, when the lambda is evaluated, it will correctly refer to the captured variable (even though the parent did not come along for the ride).
So, back to the gui example. In LW, a gui is a class. The class can contain any number of custom slots (e.g. if building an editor, you might include a list of editor buffers as instance variables). The lambda callbacks could refer to such slots as needed.
The overall result is less coding and less complexity and more flexibility.
pt
On Fri, Oct 15, 2010 at 11:39 PM, Aleksandar Matijaca amatijaca@gmail.com 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.
Let me point at the converse position.
If you have a function that isn't going to be reused, or which is only to be referenced from a particular place in your code, why do you want waste your function namespace on giving a name to that function?
The word "lambda" muddies matters a bit, as it sounds somewhat magical. Smalltalk calls the analogous thing an "anonymous block," which is a small step away from "anonymous function," which is a more nicely descriptive term.
PostgreSQL recently introduced "anonymous functions" http://wiki.postgresql.org/wiki/What's_new_in_PostgreSQL_9.0#Anonymous_Functions_.28aka_Anonymous_Blocks.29 which are billed as a good thing as they mean the database doesn't need to get cluttered up with maintenance functions that were supposed to go away almost immediately anyways.
Requiring that functions have names imposes some burdens, particularly for maintenance-like activities: - You need to come up with a name that isn't already there (lest you overwrite something, and make others unhappy) - You use that name... Which is well and fine... - Something needs to come by later to clean that name and function out, and it surely needs to use the right name (lest you drop the WRONG function!)
Anonymous functions do not impose any of this clutter.
- You pass a reference to the function to whatever needs it, so it is only accessible to "legitimate" users of the function.
- Whether by lexical or dynamic scope, the function can go away, with assurance of safety, once it is no longer needed.
- You don't need to come up with a naming convention if it has no name.
- No clutter in the namespace if the function does not occupy a name in the namespace.
And effectively, what Lisp is doing is to separate two activities: a) Creating a function, and b) Naming a function.
Creating a function *ALWAY* involves LAMBDA, behind the scenes; it may be hidden by macros, but every function is a "lambda function."
Some functions get named (as in b).
Lisp doesn't force you to name a function merely because you're creating it.
All excellent answers thank you so much. It all makes much more sense now. I did use these kinds of anonymous functions already in other languages for similar applications.
Cheers, Alex.
Sent from my iPhone
On 2010-10-16, at 12:14, Christopher Browne cbbrowne@gmail.com wrote:
On Fri, Oct 15, 2010 at 11:39 PM, Aleksandar Matijaca amatijaca@gmail.com 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.
Let me point at the converse position.
If you have a function that isn't going to be reused, or which is only to be referenced from a particular place in your code, why do you want waste your function namespace on giving a name to that function?
The word "lambda" muddies matters a bit, as it sounds somewhat magical. Smalltalk calls the analogous thing an "anonymous block," which is a small step away from "anonymous function," which is a more nicely descriptive term.
PostgreSQL recently introduced "anonymous functions" http://wiki.postgresql.org/wiki/What's_new_in_PostgreSQL_9.0#Anonymous_Functions_.28aka_Anonymous_Blocks.29 which are billed as a good thing as they mean the database doesn't need to get cluttered up with maintenance functions that were supposed to go away almost immediately anyways.
Requiring that functions have names imposes some burdens, particularly for maintenance-like activities:
- You need to come up with a name that isn't already there (lest you
overwrite something, and make others unhappy)
- You use that name... Which is well and fine...
- Something needs to come by later to clean that name and function
out, and it surely needs to use the right name (lest you drop the WRONG function!)
Anonymous functions do not impose any of this clutter.
- You pass a reference to the function to whatever needs it, so it is
only accessible to "legitimate" users of the function.
- Whether by lexical or dynamic scope, the function can go away, with
assurance of safety, once it is no longer needed.
You don't need to come up with a naming convention if it has no name.
No clutter in the namespace if the function does not occupy a name
in the namespace.
And effectively, what Lisp is doing is to separate two activities: a) Creating a function, and b) Naming a function.
Creating a function *ALWAY* involves LAMBDA, behind the scenes; it may be hidden by macros, but every function is a "lambda function."
Some functions get named (as in b).
Lisp doesn't force you to name a function merely because you're creating it.
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@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) X CL-USER> (x) FOO CL-USER> (setf (symbol-function 'x) (lambda () 'bar)) #<Anonymous Function #x302000C94FFF> CL-USER> (x) BAR CL-USER>
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.
Best,
- Dave -
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 ,@args))
(defclass my-list (capi:list-panel) () (:default-initargs :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.
BC
From: Dave Penton [mailto:djp@arqux.com] Sent: Monday, October 18, 2010 10:42 PM To: Aleksandar Matijaca Cc: toronto-lisp@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@common-lisp.netmailto:toronto-lisp@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) X CL-USER> (x) FOO CL-USER> (setf (symbol-function 'x) (lambda () 'bar)) #<Anonymous Function #x302000C94FFF> CL-USER> (x) BAR CL-USER>
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.
Best,
- Dave -