From: Bill Birch
Subject: Anaphoric Lambda?
Date: 
Message-ID: <351ADCF7.55D89FEF@ctp.com>
Paul Graham's book "On Lisp" has a macro that binds the variable self to
the current function. This gives anonymous functions without recourse to
labels. A recursive anonymous function calls itself::

(lambda (x)
     ...body...
    (self ....args...))


This seems to be very simple. So

* Why not add this to the interpreter/compiler? The Interpreter always
knows which function it is currently applying...

* Was this ever considered back in the early days?

* Was this ever considered in the standards?

Bill

From: ······@exploited.barmy.army
Subject: Re: Anaphoric Lambda?
Date: 
Message-ID: <6fej1k$ah2$1@Masala.CC.UH.EDU>
In article <·················@ctp.com>, Bill Birch  <······@ctp.com> wrote:

[Snip -- about a recursive lambda definition]

>* Why not add this to the interpreter/compiler? The Interpreter always
>knows which function it is currently applying...

While I don't know why this was left out of the system, I have
found such a capability to be unnecessary in my (brief)
experience with Common Lisp for the following reason:

	Lambda expressions tend to exist so you can pass simple
	"one-time" functions to other functions, and these other
	functions will take care of the recursion for you (like
	mapcar, reduce, remove-if, etc...).  

So by their very nature, lambda expressions tend not to be 
recursive, hence something like (self) is not needed.

This has all been in my experience, and keep in mind that
I program functionally, which means that all my iteration
is performed by higher order functions (like mapcar,
etc...) or by recursion (I don't use looping).

I do tend to use maps, reductions, and filters very heavily --
possibly too heavily, so keep this in mind.

[Snip]

>
>Bill
>
>
>



-- 
Ahmed

To respond via email, send email to punkrock at cs dot uh dot edu
From: Aaron Leung
Subject: Re: Anaphoric Lambda?
Date: 
Message-ID: <6ffitg$597$1@news.ncal.verio.com>
Hi everyone,

>Paul Graham's book "On Lisp" has a macro that binds the variable self to
>the current function. This gives anonymous functions without recourse to
>labels. A recursive anonymous function calls itself::


    I haven't read 'On Lisp', I'm more familiar with Scheme, and I'm not too
experienced in these matters, so I might not know what I'm talking about.
There; now my disclaimer's out of the way.
    Is this macro syntactic sugar for the Y combinator?  For instance,
without using define, we can write factorial as:

(lambda (n)
  ((lambda (f)
     (f f n))
   (lambda (self x)
     (if (= k 0)
         1
         (* x (self self (- x 1)))))))

Does Graham's macro do something like this?

>* Was this ever considered back in the early days?


    I think I read somewhere that, since Lisp was mostly dynamically scoped
in the early days, this issue was easier to deal with.  In dynamically
scoped Scheme, wouldn't the following simple program work?

(let ((fact (lambda (n)   ;not letrec or labels
              (if (= n 0)
                  1
                  (* n (fact (- n 1)))))))
  (fact 10))

Best regards,
Aaron
From: Rolf-Thomas Happe
Subject: Re: Anaphoric Lambda?
Date: 
Message-ID: <r5wwde22wp.fsf@indi8.mathematik.uni-freiburg.de>
In article <············@news.ncal.verio.com> "Aaron Leung" writes:
   >Paul Graham's book "On Lisp" has a macro that binds the variable self to
   >the current function. This gives anonymous functions without recourse to
   >labels. A recursive anonymous function calls itself::
[...]
       Is this macro syntactic sugar for the Y combinator?  For instance,

No.  The macro just uses LABELS (which corresponds to Scheme's LETREC,
roughly):

(defmacro alambda (parms &body body)
  `(labels ((self ,parms ,@body))
     #'self))

; This code is copyright 1993 by Paul Graham, but anyone who wants 
; to use the code in any nonprofit activity, or distribute free
; verbatim copies (including this notice), is encouraged to do so.

rthappe

PS:  Paul Graham's book code is available online.  E.g. as
ftp://ftp.cs.umn.edu//dept/users/gini/aicode/graham/onlisp.lisp