From: Dingbat Charlie
Subject: simple lambda closure notation question (Noob!)
Date: 
Message-ID: <pan.2007.02.24.21.26.21.955782@nowhere.org>
Hail Gurus,

Just a simple question.  Been going through some online tutorials and I am
a bit confused about the use of #'

What is the ddifference between:

(remove-if-not) #'(lambda (x) (= 1 (mod x 2))) '(1 2 3 4 5 6 7 8 9 10)

and

(remove-if-not) (lambda (x) (= 1 (mod x 2))) '(1 2 3 4 5 6 7 8 9 10)

Thanks in advance

From: ivant
Subject: Re: simple lambda closure notation question (Noob!)
Date: 
Message-ID: <1172354082.905234.256990@t69g2000cwt.googlegroups.com>
On Feb 24, 11:26 pm, Dingbat Charlie <····@nowhere.org> wrote:
> Hail Gurus,
>
> Just a simple question.  Been going through some online tutorials and I am
> a bit confused about the use of #'
>
> What is the ddifference between:
>
> (remove-if-not) #'(lambda (x) (= 1 (mod x 2))) '(1 2 3 4 5 6 7 8 9 10)
>
> and
>
> (remove-if-not) (lambda (x) (= 1 (mod x 2))) '(1 2 3 4 5 6 7 8 9 10)
>
> Thanks in advance

In Common Lisp one symbol can have a function value and a "normal"
value at the same time.  That is, functions and normal values are in
different name spaces.  You can see this sometimes described as Common
Lisp being a Lisp-2.

When you write (f x y), the default context for `f' is the functional
one and for `x' and `y' it's the "normal" one.  Sometimes you want to
override this and e.g. get the functional context of `x'.  You do that
by writing (f (function x) y), which can be abbreviated to (f #'x y).

In your specific example the lambda expression is specifically defined
to be the same as the same #' expression.[1]  You don't actually save
much typing here, so I guess (a wild guess) the reason behind this is
compatibility with older Lisps.  For all other functions you need to
use the #' so you can use it with the lambda form as well.


[1] http://www.lispworks.com/documentation/HyperSpec/Body/m_lambda.htm
From: Dingbat Charlie
Subject: Re: simple lambda closure notation question (Noob!)
Date: 
Message-ID: <pan.2007.02.24.22.15.56.429758@nowhere.org>
On Sat, 24 Feb 2007 13:54:42 -0800, ivant wrote:

> On Feb 24, 11:26 pm, Dingbat Charlie <····@nowhere.org> wrote:
>> Hail Gurus,
>>
>> Just a simple question.  Been going through some online tutorials and I
>> am a bit confused about the use of #'
>>
>> What is the ddifference between:
>>
>> (remove-if-not) #'(lambda (x) (= 1 (mod x 2))) '(1 2 3 4 5 6 7 8 9 10)
>>
>> and
>>
>> (remove-if-not) (lambda (x) (= 1 (mod x 2))) '(1 2 3 4 5 6 7 8 9 10)
>>
>> Thanks in advance
> 
> In Common Lisp one symbol can have a function value and a "normal" value
> at the same time.  That is, functions and normal values are in different
> name spaces.  You can see this sometimes described as Common Lisp being a
> Lisp-2.
> 
> When you write (f x y), the default context for `f' is the functional one
> and for `x' and `y' it's the "normal" one.  Sometimes you want to override
> this and e.g. get the functional context of `x'.  You do that by writing
> (f (function x) y), which can be abbreviated to (f #'x y).
> 
> In your specific example the lambda expression is specifically defined to
> be the same as the same #' expression.[1]  You don't actually save much
> typing here, so I guess (a wild guess) the reason behind this is
> compatibility with older Lisps.  For all other functions you need to use
> the #' so you can use it with the lambda form as well.
> 
> 
> [1] http://www.lispworks.com/documentation/HyperSpec/Body/m_lambda.htm

Thank you both so much for the quick answers.  That makes much more
sense to me.  I was a bit surprised when the one without the #' worked the
same way.
From: Tel A.
Subject: Re: simple lambda closure notation question (Noob!)
Date: 
Message-ID: <1172355474.784439.277700@j27g2000cwj.googlegroups.com>
On Feb 24, 4:26 pm, Dingbat Charlie <····@nowhere.org> wrote:
> Hail Gurus,
>
> Just a simple question.  Been going through some online tutorials and I am
> a bit confused about the use of #'
>
> What is the ddifference between:
>
> (remove-if-not) #'(lambda (x) (= 1 (mod x 2))) '(1 2 3 4 5 6 7 8 9 10)
>
> and
>
> (remove-if-not) (lambda (x) (= 1 (mod x 2))) '(1 2 3 4 5 6 7 8 9 10)
>
> Thanks in advance

#' is a simple read macro. It does nothing more than get expanded
during read time like this:

#'<form>  ===> (function <form>)

The tricky part comes in understanding what LAMBDA is and how FUNCTION
gives it meaning. Not to step on any toes, but here is how I
understand it. LAMBDA is nothing more than a symbol which indicates
that the following forms are defining an anonymous function. When it
is seen inside a list in FUNCTION's parameter, FUNCTION interprets
that as an anonymous function and generates the correct object.
Outside of FUNCTION lambda should have no meaning to lisp. It does,
however, have meaning to the writer of the code, so in Common Lisp
LAMBDA is defined as a macro as well. In context outside of FUNCTION
it will expand into a call to FUNCTION:

CL-USER> (macroexpand-1 '(lambda (x) (+ 1 x)))
#'(LAMBDA (X) (+ 1 X))
NIL

So, to make a long answer short, 99% of the time there is nothing
different between #'(lambda ...) and (lambda ...) alone.
From: Vassil Nikolov
Subject: Re: simple lambda closure notation question (Noob!)
Date: 
Message-ID: <yy8vwt27hy45.fsf@eskimo.com>
On 24 Feb 2007 14:17:54 -0800, "Tel A." <············@gmail.com> said:
| ...
| Outside of FUNCTION lambda should have no meaning to lisp. It does,

  This is true as a first approximation, but see notes below.

| however, have meaning to the writer of the code, so in Common Lisp
| LAMBDA is defined as a macro as well.

  This was done relatively late in Common Lisp's history to make it
  culturally more compatible with Scheme (which does not need and
  does not have a FUNCTION special form), or to avoid #' clutter in
  higher-order programming if you prefer to put it that way.  That was
  somewhat controversial; there are people who would always write
  #'(lambda ...) in a for-evaluation position in Common Lisp.  In a
  broader perspective, this can be considered a part of the great
  Lisp-1 vs. Lisp-2 debate.

  There is a good amount of literature explaining why the FUNCTION
  special form was introduced in lisp.  One noteworthy article is
  "The function of FUNCTION in LISP" (I hope it is googlable).

  Notes:

  1. A lambda expression can appear as the first element of a lambda
  form, in which case LAMBDA is not treated as a macro; this is one
  of lisp's lambda-calculus roots:

    ((lambda (x) (+ x 1)) 3) => 4

  ---lambda forms are rarely used, but can be very useful on occasion.

  2. COERCE and COMPILE each can take a lambda expression as one of
  their arguments (as one possibility).  In this way a function can
  be produced from a list at runtime.

  ---Vassil.


-- 
Our programs do not have bugs; it is just that the users' expectations
differ from the way they are implemented.
From: Steven Haflich
Subject: Re: simple lambda closure notation question (Noob!)
Date: 
Message-ID: <9m6Eh.2896$re4.679@newssvr12.news.prodigy.net>
Vassil Nikolov wrote:

> | however, have meaning to the writer of the code, so in Common Lisp
> | LAMBDA is defined as a macro as well.
> 
>   This was done relatively late in Common Lisp's history to make it
>   culturally more compatible with Scheme

This is factually incorrect.  Vassil, I don't remember you being present
when we (X3J13) voted this change.  The addition had little to do with
Scheme.

The lambda macro was one of the last substantive changes made by X3J13
as the draft standard was being finalized.  The exact same issue had
been voted and rejected a couple years earlier.  (The rejection was by 
majority vote, but if I remember correctly the decision was not nearly
unanimous.)  However, in 1993 Kent Pitman became concerned that it
should be possible to emulate ISO Lisp efficiently in CL, but the 
prohibition against defining a macro on exported common-lisp package
symbols made emulation of ISO Lisp lambda very difficult.

I had earlier voted against the lambda macro, since by hiding what was
going on it seemed to add confusion rather than reduce it, but the ISO
Lisp argument was compelling so I voted for it in 1993.  It's hard to
remember after so many years, but I believe the second vote was
unanimous or nearly unanimous.

The other incompatibility was the lack of define-symbol-macro. 
symbol-macrolet was already in he language, but here was no way to
define a global symbol macro.  All this is documented in
http://www.lispworks.com/documentation/HyperSpec/Issues/iss198_w.htm
From: Vassil Nikolov
Subject: Re: simple lambda closure notation question (Noob!)
Date: 
Message-ID: <yy8vy7mmnc6t.fsf@eskimo.com>
On Sun, 25 Feb 2007 02:18:45 GMT, Steven Haflich <···@alum.mit.edu> said:

| Vassil Nikolov wrote:
|| ...
|| [about defining the LAMBDA macro] to make [Common Lisp]
|| culturally more compatible with Scheme

| This is factually incorrect.  Vassil, I don't remember you being present
| when we (X3J13) voted this change.  The addition had little to do with
| Scheme.

  I stand corrected.  Sorry.

  ---Vassil.


-- 
Our programs do not have bugs; it is just that the users' expectations
differ from the way they are implemented.
From: Rainer Joswig
Subject: Re: simple lambda closure notation question (Noob!)
Date: 
Message-ID: <joswig-D9E601.08564625022007@news-europe.giganews.com>
In article <··················@newssvr12.news.prodigy.net>,
 Steven Haflich <···@alum.mit.edu> wrote:

> Vassil Nikolov wrote:
> 
> > | however, have meaning to the writer of the code, so in Common Lisp
> > | LAMBDA is defined as a macro as well.
> > 
> >   This was done relatively late in Common Lisp's history to make it
> >   culturally more compatible with Scheme
> 
> This is factually incorrect.  Vassil, I don't remember you being present
> when we (X3J13) voted this change.  The addition had little to do with
> Scheme.
> 
> The lambda macro was one of the last substantive changes made by X3J13
> as the draft standard was being finalized.  The exact same issue had
> been voted and rejected a couple years earlier.  (The rejection was by 
> majority vote, but if I remember correctly the decision was not nearly
> unanimous.)  However, in 1993 Kent Pitman became concerned that it
> should be possible to emulate ISO Lisp efficiently in CL, but the 
> prohibition against defining a macro on exported common-lisp package
> symbols made emulation of ISO Lisp lambda very difficult.
> 
> I had earlier voted against the lambda macro, since by hiding what was
> going on it seemed to add confusion rather than reduce it, but the ISO
> Lisp argument was compelling so I voted for it in 1993.  It's hard to
> remember after so many years, but I believe the second vote was
> unanimous or nearly unanimous.
> 
> The other incompatibility was the lack of define-symbol-macro. 
> symbol-macrolet was already in he language, but here was no way to
> define a global symbol macro.  All this is documented in
> http://www.lispworks.com/documentation/HyperSpec/Issues/iss198_w.htm

I guess compatibility with 'ISO Lisp' or ISLisp is no longer
something to worry about. 

http://islisp.info/
From: Pascal Costanza
Subject: Re: simple lambda closure notation question (Noob!)
Date: 
Message-ID: <54bqi8F1uveiqU1@mid.individual.net>
Dingbat Charlie wrote:
> Hail Gurus,
> 
> Just a simple question.  Been going through some online tutorials and I am
> a bit confused about the use of #'
> 
> What is the ddifference between:
> 
> (remove-if-not) #'(lambda (x) (= 1 (mod x 2))) '(1 2 3 4 5 6 7 8 9 10)
> 
> and
> 
> (remove-if-not) (lambda (x) (= 1 (mod x 2))) '(1 2 3 4 5 6 7 8 9 10)

(lambda (...) ...) and #'(lambda (...) ...) are equivalent. The latter 
is more "precise", and the former is a convenience macro that just saves 
two key strokes.


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/