From: sergio GARCIA
Subject: quiestion on lambda
Date: 
Message-ID: <cb9fdb$9i7$1@trompette.imag.fr>
Hello, I'm learning Lisp and I have the following question:

Is there any practical difference between i.e. :

(funcall (lambda (x) (* x x)) 3)

and (notice the #') :

(funcall #'(lambda (x) (* x x)) 3)

Thanks, 

Sergio

From: Ari Johnson
Subject: Re: quiestion on lambda
Date: 
Message-ID: <jyXBc.25184$8r5.24837@fed1read03>
sergio GARCIA wrote:
> Hello, I'm learning Lisp and I have the following question:
> 
> Is there any practical difference between i.e. :
> 
> (funcall (lambda (x) (* x x)) 3)
> 
> and (notice the #') :
> 
> (funcall #'(lambda (x) (* x x)) 3)

Nope, no practical differences.  Lambda is a macro that expands to 
#'(lambda ...).  I suspect that the choice is up to what will become 
your personal sense style in Lisp.

[1]> (macroexpand-1 '(lambda (x) (* x x)))
#'(LAMBDA (X) (* X X)) ;
T
From: Thomas Bakketun
Subject: Re: quiestion on lambda
Date: 
Message-ID: <pan.2004.06.23.12.40.32.277993@kokusbolle.bakketun.net>
* Ari Johnson:

> Nope, no practical differences.  Lambda is a macro that expands to 
> #'(lambda ...).  

No, the lambda macro expands to (function (lambda ...)).  

> [1]> (macroexpand-1 '(lambda (x) (* x x)))
> #'(LAMBDA (X) (* X X)) ;
> T

What happens here is that (lambda (x) (* x x)) really expands to 
(function (lambda (x) (* x x))), but this fact is hidden when the form i
printed.  This can be seen by:

CL-USER> (car (macroexpand-1 '(lambda (x) (* x x))))
FUNCTION

Or we can build the list manually:

CL-USER> (list 'function (list 'lambda '(x) '(* x x)))
#'(LAMBDA (X) (* X X))

-- 
Roten til alt vondt er egen syntaks for attributter.
From: Joe Marshall
Subject: Re: quiestion on lambda
Date: 
Message-ID: <smcnk02b.fsf@ccs.neu.edu>
sergio GARCIA <·············@imag.fr> writes:

> Hello, I'm learning Lisp and I have the following question:
>
> Is there any practical difference between i.e. :
>
> (funcall (lambda (x) (* x x)) 3)
>
> and (notice the #') :
>
> (funcall #'(lambda (x) (* x x)) 3)

The latter requires typing two more characters, one of which is
shifted.  The latter also may make indentation creep to the right
slightly quicker.

Semantically, however, they are virtually identical.
From: Erann Gat
Subject: Re: quiestion on lambda
Date: 
Message-ID: <gNOSPAMat-294C84.11012322062004@nntp1.jpl.nasa.gov>
In article <············@ccs.neu.edu>, Joe Marshall <···@ccs.neu.edu> 
wrote:

> sergio GARCIA <·············@imag.fr> writes:
> 
> > Hello, I'm learning Lisp and I have the following question:
> >
> > Is there any practical difference between i.e. :
> >
> > (funcall (lambda (x) (* x x)) 3)
> >
> > and (notice the #') :
> >
> > (funcall #'(lambda (x) (* x x)) 3)
> 
> The latter requires typing two more characters, one of which is
> shifted.  The latter also may make indentation creep to the right
> slightly quicker.
> 
> Semantically, however, they are virtually identical.

Well, we can turn this into a little bit more of a learning opportunity.

One must distinguish between lambda expressions and functions.

A lambda expression is a list whose CAR is the symbol LAMBDA.

A function is an opaque data structure that can be called, i.e. it can 
legally be passed as the first argument to FUNCALL or APPLY.

Lambda expressions are lists, not functions.  Lambda expressions cannot 
be called.  They can, however, be converted into functions using the 
FUNCTION special form.

So, to review:

'(lambda ...) --> a lambda expression, i.e. a list (not callable)
(function (lambda ...)) --> a function (callable)

(Exercise: what happens if you type (function '(lambda ...))?  Why?)

Because typing (function (lambda ...)) gets annoying, there are two 
shortcuts: a reader macro, and a regular macro.  The reader macro is the 
#. macro and the regular macro is called (confusingly) LAMBDA.

So #'(lambda ...) expands into (function (lambda ...)) AT READ TIME.  
(lambda ...) expands into (function (lambda ...)) AT MACROEXPANSION TIME.

E.
From: Ari Johnson
Subject: Re: quiestion on lambda
Date: 
Message-ID: <h_4Cc.50$iU6.44@fed1read03>
Erann Gat wrote:

> So #'(lambda ...) expands into (function (lambda ...)) AT READ TIME.  
> (lambda ...) expands into (function (lambda ...)) AT MACROEXPANSION TIME.

Is there a time when this distinction of when the expansion to (function 
(lambda ...)) occurs makes a difference?
From: Joe Marshall
Subject: Re: quiestion on lambda
Date: 
Message-ID: <llievben.fsf@comcast.net>
Ari Johnson <·····@hotmail.com> writes:

> Erann Gat wrote:
>
>> So #'(lambda ...) expands into (function (lambda ...)) AT READ TIME.
>> (lambda ...) expands into (function (lambda ...)) AT MACROEXPANSION
>> TIME.
>
> Is there a time when this distinction of when the expansion to
> (function (lambda ...)) occurs makes a difference?

Yes, but it is rather obscure.  I've only encountered it in a code
walker that was looking specifically for (function (lambda ...)).
When it saw just the (lambda ...), it fussed about it.  It was simple
enough to modify the original code to check for both.

-- 
~jrm
From: Alex Mizrahi
Subject: Re: quiestion on lambda
Date: 
Message-ID: <2jt1p3F15g35hU1@uni-berlin.de>
(message (Hello 'Ari)
(you :wrote  :on '(Tue, 22 Jun 2004 18:23:42 -0700))
(

 >> So #'(lambda ...) expands into (function (lambda ...)) AT READ TIME.
 >> (lambda ...) expands into (function (lambda ...)) AT MACROEXPANSION
 >> TIME.

 AJ> Is there a time when this distinction of when the expansion to
 AJ> (function  (lambda ...)) occurs makes a difference?

when it's quoted, i think. read time expansion will be done at that time,
but macroexpansion will not..

)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
(prin1 "Jane dates only Lisp programmers"))
From: Erann Gat
Subject: Re: quiestion on lambda
Date: 
Message-ID: <gNOSPAMat-547EDA.15091123062004@nntp1.jpl.nasa.gov>
In article <···············@fed1read03>,
 Ari Johnson <·····@hotmail.com> wrote:

> Erann Gat wrote:
> 
> > So #'(lambda ...) expands into (function (lambda ...)) AT READ TIME.  
> > (lambda ...) expands into (function (lambda ...)) AT MACROEXPANSION TIME.
> 
> Is there a time when this distinction of when the expansion to (function 
> (lambda ...)) occurs makes a difference?

Yes, but they're pretty obscure.  For example, if you're writing a macro 
`#'(lambda ...) might work in some cases where `(lambda ...) wouldn't, 
and verse-visa.  It occurred to me after posting my original reply that 
lambda expressions are callable in one special circumstance in CL -- 
when one appears as the CAR of a list.  So ((lambda ...) ...) is legal, 
but (#'(lambda ...) ...) isn't.

E.
From: Timothy Moore
Subject: Re: quiestion on lambda
Date: 
Message-ID: <wdrpt7rmlql.fsf@serveur5.labri.fr>
Joe Marshall <···@ccs.neu.edu> writes:

> sergio GARCIA <·············@imag.fr> writes:
> 
> > Hello, I'm learning Lisp and I have the following question:
> >
> > Is there any practical difference between i.e. :
> >
> > (funcall (lambda (x) (* x x)) 3)
> >
> > and (notice the #') :
> >
> > (funcall #'(lambda (x) (* x x)) 3)
> 
> The latter requires typing two more characters, one of which is
> shifted.  The latter also may make indentation creep to the right
> slightly quicker.
> 
> Semantically, however, they are virtually identical.

The fact that in the first case lambda is a macro, and the corollary
that the symbol LAMBDA in that case is subject to macroexpansion, can
be be used to perform various metering and debugging tricks. For
example, in conjunction with a custom defun macro you could shadow
COMMON-LISP:LAMBDA with your own macro that captured the name of the
surrounding function, if any. That is harder to do with the #' reader
macro without getting very intimate with your implementation.

On the other hand, if you implement my trick you'll effectively break
#'. You can get around this by modifying the system's macro definition
of lambda, but... well, proceed with care :)

Tim
From: Tim Bradshaw
Subject: Re: quiestion on lambda
Date: 
Message-ID: <fbc0f5d1.0406230317.651dba72@posting.google.com>
Timothy Moore <·····@serveur5.labri.fr> wrote in message news:<···············@serveur5.labri.fr>...

> 
> The fact that in the first case lambda is a macro, and the corollary
> that the symbol LAMBDA in that case is subject to macroexpansion, can
> be be used to perform various metering and debugging tricks. For
> example, in conjunction with a custom defun macro you could shadow
> COMMON-LISP:LAMBDA with your own macro that captured the name of the
> surrounding function, if any. That is harder to do with the #' reader
> macro without getting very intimate with your implementation.

I'm not sure if you meant this by the bit I've elided, but doing this
is not likely to work.  if (not (eq 'lambda 'cl:lambda)) then
#'(lambda ...) will not work, and nor will ((lambda (x) x) 1).  That's
a fairly heavy price to pay, I think.

(This is analogous to shadowing EQL: if you do it, then EQL methods
won't work any more, or rather they'll be hard to type, because
they're now CL:EQL methods.  Namespace nerds will point out that if CL
had a proper module system none of this would be a problem.  The spec
would be 4000 pages longer and full of formal semantics though, and
no-one would be able to understand the module system or, in fact, use
the langiuage at all.  This is a small price to pay if you're a
namespace nerd.)

--tim
From: Timothy Moore
Subject: Re: quiestion on lambda
Date: 
Message-ID: <wdrd63qcde5.fsf@serveur5.labri.fr>
··········@tfeb.org (Tim Bradshaw) writes:

> Timothy Moore <·····@serveur5.labri.fr> wrote in message
> news:<···············@serveur5.labri.fr>...
> 
> > 
> > The fact that in the first case lambda is a macro, and the corollary
> > that the symbol LAMBDA in that case is subject to macroexpansion, can
> > be be used to perform various metering and debugging tricks. For
> > example, in conjunction with a custom defun macro you could shadow
> > COMMON-LISP:LAMBDA with your own macro that captured the name of the
> > surrounding function, if any. That is harder to do with the #' reader
> > macro without getting very intimate with your implementation.
> 
> I'm not sure if you meant this by the bit I've elided, but doing this
> is not likely to work.  if (not (eq 'lambda 'cl:lambda)) then
> #'(lambda ...) will not work, and nor will ((lambda (x) x) 1).  That's
> a fairly heavy price to pay, I think.

I did mention breaking #'(lambda ...) and noted that it would be a
pain to get around that, though of course only in your own
packages. You could adopt a rule of only using (lambda ) and not
#'(lambda ), which you'd need to do to benefit from the kind of things
I'm talking about.

I forgot completely about ((lambda (x) ...) ...). Yup, my scheme would
break that unless you typed ((cl:lambda (...) ...) ...). I hardly ever use
that syntax.

The Other Tim
From: Tayssir John Gabbour
Subject: Re: quiestion on lambda
Date: 
Message-ID: <866764be.0406231153.5e0ee2d6@posting.google.com>
Joe Marshall <···@ccs.neu.edu> wrote in message news:<············@ccs.neu.edu>...
> sergio GARCIA <·············@imag.fr> writes:
> > (funcall (lambda (x) (* x x)) 3)
> >
> > and (notice the #') :
> >
> > (funcall #'(lambda (x) (* x x)) 3)
> 
> Semantically, however, they are virtually identical.

I'm surprised there wasn't a big deal about making lambda into a
special operator. But that's probably because I still have too much
Schemer dna. ;)

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&safe=off&selm=SMH.94Jul30001530%40vapor.Franz.COM

Would've thought that the main consideration was a desire to keep the
# of special operators down, for codewalkers.

So much easier for me to think, "lambda is a special form which lets
you interact wtih lexical scope."
From: Björn Lindberg
Subject: Re: quiestion on lambda
Date: 
Message-ID: <hcsoenbr7fk.fsf@knatte.nada.kth.se>
sergio GARCIA <·············@imag.fr> writes:

> Hello, I'm learning Lisp and I have the following question:
> 
> Is there any practical difference between i.e. :
> 
> (funcall (lambda (x) (* x x)) 3)
> 
> and (notice the #') :
> 
> (funcall #'(lambda (x) (* x x)) 3)

Not really. The LAMBDA in your first example is a macro, which is
expanded into (FUNCTION (LAMBDA ...)) (ie the form in the second
example) before FUNCALL is invoked. FUNCTION then is a special
operator which takes either a function name or a lambda expression. In
this context the LAMBDA is thus not macroexpanded.


Bj�rn