From: Peter Seibel
Subject: (lambda ()) vs #'(lambda ())
Date: 
Message-ID: <m2wuyvf1tn.fsf@javamonkey.com>
So I'm confused; is there a difference between (lambda ()) and
#'(lambda ())? (Other than the obvious syntactic one ;-)). I was
reading Paradigms in AI Programming and he says that a lambda
expression is just a non-atomic name for an anonymous function.  Thus
you can write ((lambda (x) (* 2 x)) 4) and it will evaluate to
8. Therefore, you should write (mapcar #'(lambda ())) rather than
(mapcar (lambda())). Yet according to CLISP (FWIW; obviously an
implementation isn't the spec.):

 [1]> (type-of (lambda ()))
 FUNCTION
 [2]> (type-of #'(lambda ()))
 FUNCTION
 [3]> (functionp (lambda ()))
 T
 [4]> (functionp #'(lambda ()))
 T
 [5]> (funcall (lambda () 1))
 1
 [6]> (funcall #'(lambda () 1))
 1

-Peter

From: Kaz Kylheku
Subject: Re: (lambda ()) vs #'(lambda ())
Date: 
Message-ID: <R76_7.6630$sb.1186799@news1.calgary.shaw.ca>
In article <··············@javamonkey.com>, Peter Seibel wrote:
>So I'm confused; is there a difference between (lambda ()) and
>#'(lambda ())? (Other than the obvious syntactic one ;-)).

The first is a standard macro which expands to the second. The handy macro
allows you to dispense with the #' notation, if you want.

>I was
>reading Paradigms in AI Programming and he says that a lambda
>expression is just a non-atomic name for an anonymous function.

Strictly speaking, this isn't right; a lambda expression is not considered
a function name. (See ``function name'' in the Common Lisp HyperSpec
glossary).

  Thus
>you can write ((lambda (x) (* 2 x)) 4) and it will evaluate to
>8.

In this context, the lambda expression appears in the first position
of a list expression that is evaluated.  This position is subject to
different evaluation rules. The lambda macro does not come into effect;
the lambda expression is simply recognized as denoting a function.
If the macro came into effect, that wouldn't work, because

  (#'(lambda (x) (* 2 x)) 4) 

is not valid.

In other contexts where it is evaluated, you obtain a funcallable object,
a closure, thanks to the lambda macro which brings in the function operator.
That function operator's job is to create the funcallable object from 
a function name or from a lambda expression.

>Therefore, you should write (mapcar #'(lambda ())) rather than
>(mapcar (lambda())).

Many programmers write #' anyway.
From: Michael J. Ferrador
Subject: Recursive Anonymous Lambda Was: (lambda ()) vs #'(lambda ())
Date: 
Message-ID: <Y4a_7.1210$jk5.180709@news02.optonline.net>
Has anyone ever thought it would be nice to recurse an anonymous lambda?

Or are recursive functions important enough to have (be bound to) a name.


I was thinking about a symbol recurse, always bound to the current lambda

But then it is not exactly anonymous any more, is it?


Maybe I was cutting and pasting functions, renaming,
BUT forgetting to rename the inner recursion(s).

Is this an editor / x-ref / emacs job?


> In article <··············@javamonkey.com>, Peter Seibel wrote:
>
> >I was
> >reading Paradigms in AI Programming and he says that a lambda
> >expression is just a non-atomic name for an anonymous function.

I'm still chewing on the non-atomic part, maybe It's too late at night

> Strictly speaking, this isn't right; a lambda expression is not considered
> a function name. (See ``function name'' in the Common Lisp HyperSpec
> glossary).

---

And I'd like to thank ftp servers everywhere
for forcing me to learn to spell anonymous...
From: Christopher Stacy
Subject: Re: Recursive Anonymous Lambda Was: (lambda ()) vs #'(lambda ())
Date: 
Message-ID: <uofk6zib3.fsf@spacy.Boston.MA.US>
>>>>> On Mon, 07 Jan 2002 05:10:16 GMT, Michael J Ferrador ("Michael") writes:
 Michael> Has anyone ever thought it would be nice to recurse an anonymous lambda?
 Michael> Or are recursive functions important enough to have (be bound to) a name.

That's what LABELS is for.

Or you could pass the function to itself:

(defun factorializationism (n)
  (let ((fact #'(lambda (f x)
                 (if (= x 1) 1 (* x (funcall f f (- x 1)))))))
    (funcall fact fact n)))
From: James McDonald
Subject: Re: Recursive Anonymous Lambda Was: (lambda ()) vs #'(lambda ())
Date: 
Message-ID: <3C3A69DE.9090509@kt-llc.com>
Michael J. Ferrador wrote:

 > Has anyone ever thought it would be nice to recurse an anonymous lambda?

You might want to research the Y combinator.  Dick Gabriel wrote a nice 
little paper called "The Why of Y" (or something close).

Sorry I'm to pressed for time to say more now, except to warn you that
the concepts related to fixpoint combinators are subtle (especially the 
implementations of them!) and it may take a bit of patient study before 
it all makes sense, but the effort is well worth it.

James McDonald
From: Erik Naggum
Subject: Re: Recursive Anonymous Lambda Was: (lambda ()) vs #'(lambda ())
Date: 
Message-ID: <3219426877120035@naggum.net>
* "Michael J. Ferrador" <·····@orn.com>
| Has anyone ever thought it would be nice to recurse an anonymous lambda?

(defun jestcall (function &rest arg)
  (apply function function args))

  Where you would funcall a function that would funcall itself by name to
  make a recursive call, you jestcall a function that jestcalls its first
  argument to make a recursive call.

///
-- 
From: Kaz Kylheku
Subject: Re: Recursive Anonymous Lambda Was: (lambda ()) vs #'(lambda ())
Date: 
Message-ID: <H0p_7.202$Mc5.16893@news3.calgary.shaw.ca>
In article <·····················@news02.optonline.net>, Michael
J. Ferrador wrote:
>Has anyone ever thought it would be nice to recurse an anonymous lambda?
>
>Or are recursive functions important enough to have (be bound to) a name.
>
>
>I was thinking about a symbol recurse, always bound to the current lambda
>
>But then it is not exactly anonymous any more, is it?
>
>
>Maybe I was cutting and pasting functions, renaming,
>BUT forgetting to rename the inner recursion(s).

Do you know about the labels form for defining local functions that
can be recursive? Maybe that's all you want.

Also note that a function can call itself recursively using funcall,
apply or similar functions, rather than by name. It just needs to have
a function object representing itself, passed in as an argument.

E.g.

   (setf factorial #'(lambda (self arg) 
                       (if (<= arg 0) 1 (* arg (funcall self self (1- arg))))))

   (funcall factorial factorial 10)

Oh, and about your idea regarding the symbol recurse, here is a stupid
macro which lets you write closures in whose contexts you can use
the symbol recurse to refer to the function. It illustrates a
use of labels, if nothing else.

(defmacro recursive-closure ((&rest lambda-list) &body forms)
  (let ((args-bounce (gensym)))
   `#'(lambda (&rest ,args-bounce)
        (labels ((recurse (,@lambda-list) ,@forms))
          (apply #'recurse ,args-bounce)))))
From: Ikram
Subject: Re: Recursive Anonymous Lambda Was: (lambda ()) vs #'(lambda ())
Date: 
Message-ID: <80u1tw98xz.fsf@cs.pdn.ac.lk>
>>>>> Michael J Ferrador <·····@orn.com> writes:
 > Has anyone ever thought it would be nice to recurse an anonymous
 > lambda?  Or are recursive functions important enough to have (be
 > bound to) a name.

do self-referential lambdas count? :-) 

(#1=(LAMBDA (X) 
      "Factorial of X."
      (IF (= X 1) 
	  1 
	(* X (#1# (1- X)))))
  10)

I entered this into the repl expecting the worst to happen, and was
pleasantly surprised to see it evaluated in the intended way by ACL.

however CMUCL went into a consing frenzy and produced puzzling GC
messages like 
[GC completed with 37,874,208 bytes retained and -10,088 bytes freed.]
(-10,088 bytes freed?) until the Lisp process had to be killed.

-- 
I. M. Ikram                                         ·····@cs.pdn.ac.lk
From: Joe Schaefer
Subject: Re: Recursive Anonymous Lambda Was: (lambda ()) vs #'(lambda ())
Date: 
Message-ID: <m37kqtq9lw.fsf@mumonkan.sunstarsys.com>
Ikram <·····@cs.pdn.ac.lk> writes:

> do self-referential lambdas count? :-) 
> 
> (#1=(LAMBDA (X) 
>       "Factorial of X."
>       (IF (= X 1) 
> 	  1 
> 	(* X (#1# (1- X)))))
                   ^^^^^

                   X-1 ?

-- 
Joe Schaefer
From: Joe Schaefer
Subject: Re: Recursive Anonymous Lambda Was: (lambda ()) vs #'(lambda ())
Date: 
Message-ID: <m33d1hq8yk.fsf@mumonkan.sunstarsys.com>
Joe Schaefer <··········@sunstarsys.com> writes:

> Ikram <·····@cs.pdn.ac.lk> writes:
> 
> > do self-referential lambdas count? :-) 
> > 
> > (#1=(LAMBDA (X) 
> >       "Factorial of X."
> >       (IF (= X 1) 
> > 	  1 
> > 	(* X (#1# (1- X)))))
>                    ^^^^^
> 
>                    X-1 ?

Eh, sorry- insufficient caffeine level.
My mistake.

-- 
Joe Schaefer
From: Kent M Pitman
Subject: Re: Recursive Anonymous Lambda Was: (lambda ()) vs #'(lambda ())
Date: 
Message-ID: <sfwg05h6mvn.fsf@shell01.TheWorld.com>
Ikram <·····@cs.pdn.ac.lk> writes:

> >>>>> Michael J Ferrador <·····@orn.com> writes:
>  > Has anyone ever thought it would be nice to recurse an anonymous
>  > lambda?  Or are recursive functions important enough to have (be
>  > bound to) a name.
> 
> do self-referential lambdas count? :-) 
> 
> (#1=(LAMBDA (X) 
>       "Factorial of X."
>       (IF (= X 1) 
> 	  1 
> 	(* X (#1# (1- X)))))
>   10)
> 
> I entered this into the repl expecting the worst to happen, and was
> pleasantly surprised to see it evaluated in the intended way by ACL.

Yes, this might sometimes accidentally work interpreted.  Since the very
definition of interpretation is incremental (in this case, read: lazy)
application of semantics.  If you did total application of semantics, 
you'd pretty much be obliged to lose, for reasons outlined below.
So DON'T rely on it.
 
> however CMUCL went into a consing frenzy and produced puzzling GC
> messages like 
> [GC completed with 37,874,208 bytes retained and -10,088 bytes freed.]
> (-10,088 bytes freed?) until the Lisp process had to be killed.

This is more likely.  LispWorks 4.2 blows up this way, too.  
Not surprisingly.  I see no implementation bug here; the bug is the user's.
You've made a circular program.  Some implementations may catch this,
others may not.

Before you go thinking that EQ-ness is something the compiler should be 
catching in order to avoid doing gratuitous compilation, consider that
#1# had no free variables in the above situation.  Had it, though, as in
 (LET ((X 10))
   (#1=(LAMBDA () 
         (IF (= X 1) 1
             (* X (LET ((X (1- X)))
                    (#1#)))))))
in which each invocation refers to a different lexically apparent X.
Just because two expressions are EQ doesn't mean they should be compiled
the same. ;)  A simpler example to prove that is:
 (LET ((X 1) (Y 2))
   (+ X (SYMBOL-MACROLET ((X Y)) X)))
 => 3
Each X in the sum means utterly different things.

So you can't rely even on EQ (nor EQ hash tables) for circularity checking.
It's not obvious what criterion you CAN use.  The compiler needs to recurse
into each one of these things to compile it, EVEN IF it is EQ to something
seen before, and therefore (since there are an infinite number) is going to
lose without some VERY sophisticated graph matching (which would slow down
all other compilation, all REASONABLE compilation, because it should never
have to be done in the first place) and logical inferencing about certain
such matching graphs.

We all go through this silliness at some point--discovering the wonders of 
the circular statement.  Enjoy it.  Then forget it.
From: Johan Kullstam
Subject: Re: (lambda ()) vs #'(lambda ())
Date: 
Message-ID: <m3r8p21a4k.fsf@sysengr.res.ray.com>
Peter Seibel <·····@localhost.i-did-not-set--mail-host-address--so-shoot-me> writes:

> So I'm confused; is there a difference between (lambda ()) and
> #'(lambda ())? (Other than the obvious syntactic one ;-)). I was
> reading Paradigms in AI Programming and he says that a lambda
> expression is just a non-atomic name for an anonymous function.  Thus
> you can write ((lambda (x) (* 2 x)) 4) and it will evaluate to
> 8. Therefore, you should write (mapcar #'(lambda ())) rather than
> (mapcar (lambda())). Yet according to CLISP (FWIW; obviously an
> implementation isn't the spec.):

the short answer is that you can use or not use #' with lambda at your
option.  it used to be true in previous versions of common-lisp.
however, since the ansi spec it has been fudged by a little creative
macrology.  this doesn't break old code which uses #', but allows you
to not use #' with lambda.  use of #'(lambda () ..) is now more a
matter of taste and ineritia.

>  [1]> (type-of (lambda ()))
>  FUNCTION
>  [2]> (type-of #'(lambda ()))
>  FUNCTION
>  [3]> (functionp (lambda ()))
>  T
>  [4]> (functionp #'(lambda ()))
>  T
>  [5]> (funcall (lambda () 1))
>  1
>  [6]> (funcall #'(lambda () 1))
>  1
> 
> -Peter
> 
> 
> 

-- 
J o h a n  K u l l s t a m
[········@ne.mediaone.net]
sysengr
From: Dr. Edmund Weitz
Subject: Re: (lambda ()) vs #'(lambda ())
Date: 
Message-ID: <m31yh2o1v3.fsf@bird.agharta.de>
Peter Seibel <·····@localhost.i-did-not-set--mail-host-address--so-shoot-me> writes:

> So I'm confused; is there a difference between (lambda ()) and
> #'(lambda ())? (Other than the obvious syntactic one ;-)). I was
> reading Paradigms in AI Programming and he says that a lambda
> expression is just a non-atomic name for an anonymous function.
> Thus you can write ((lambda (x) (* 2 x)) 4) and it will evaluate to
> 8. Therefore, you should write (mapcar #'(lambda ())) rather than
> (mapcar (lambda())). Yet according to CLISP (FWIW; obviously an
> implementation isn't the spec.):
> 
>  [1]> (type-of (lambda ()))
>  FUNCTION
>  [2]> (type-of #'(lambda ()))
>  FUNCTION
>  [3]> (functionp (lambda ()))
>  T
>  [4]> (functionp #'(lambda ()))
>  T
>  [5]> (funcall (lambda () 1))
>  1
>  [6]> (funcall #'(lambda () 1))
>  1
> 
> -Peter

See the CLHS
<http://www.xanalys.com/software_tools/reference/HyperSpec/index.html>
entry for the macro LAMBDA. Here's an excerpt:

  "Provides a shorthand notation for a function special form involving
  a lambda expression such that:

       (lambda lambda-list [[declaration* | documentation]] form*)
   ==  (function (lambda lambda-list [[declaration* | documentation]] form*))
   ==  #'(lambda lambda-list [[declaration* | documentation]] form*)"

Edi.