[toronto-lisp] Lambda question

Christopher Browne cbbrowne at gmail.com
Sat Oct 16 16:14:02 UTC 2010


On Fri, Oct 15, 2010 at 11:39 PM, Aleksandar Matijaca
<amatijaca at 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.
-- 
http://linuxfinances.info/info/lisp.html




More information about the toronto-lisp mailing list