help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ** Graded examples of lambda functions in emacs lisp, how to create


From: David Kastrup
Subject: Re: ** Graded examples of lambda functions in emacs lisp, how to create hook variable? **
Date: 08 Oct 2002 17:13:24 +0200
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50

gnuist006@hotmail.com (gnuist006) writes:

> David Kastrup <David.Kastrup@t-online.de> wrote in message 
> news:<x5it0edz31.fsf@tupik.goethe.zz>...
> > gnuist007@hotmail.com (gnuist) writes:
> > 
> > > In the same way, I ask for GRADED examples of use of lambda. I
> > > am sure many of you can just cut and paste from your
> > > collection. Examples to illustrate recursion, etc. And how will
> > > you do recursion with/without "LABEL"?
> > 
> > ((lambda (f n) (funcall f f n))
> >  (lambda (f n) (if (zerop n) 1 (* n (funcall f f (1- n)))))
> >  5)
> 
> I know that many people intended to be helpful. But,
> this is the type of code I was looking for the one
> that runs in emacs since that is the only interpreter 
> I have running.

For lambda calculus, scheme might also be nice, and maybe you have it
there.  Try

guile

or

umb-scheme

for two commonly installed interpreters.

> However, I would want some explanatory comments.

Cough.  I hope people will not call me conceited when I say that
this, and in particular the following example are not quite trivial.

> Is funcall a primitive function of emacs lisp in the
> sense of the 5 primitive functions of JM (macarthy's) lisp?

I would guess so, more or less.  Though you could define it as
(defun funcall (lambda fun &rest args) (apply fun rest))

> Is it corresponding to the primitive "LABEL" that is
> discussed in his papers?

Unlikely.

> On the other thread today you posted this one and it runs on
> emacs. Can you give some explanatory comments/dissection?
> 
> ((lambda (f g n) (funcall g (funcall f f g) n))
>  (lambda (f g) `(lambda (n) (,g (funcall ,f ,f ,g) n)))
>  (lambda (f n) (if (zerop n) 1 (* n (funcall f (1- n)))))
>  5)
> 
> I think that of all the post this was helpful since it starts
> where my knowledge ends on this subject.

` starts a quoted expression, where elements with , before them get
evaluated and spliced into the resulted quoted expression.

The whole can be abbreviated a bit more in the following way which I
will discuss:

((lambda (f g n) (funcall (funcall f f g) n))
 (lambda (f g) `(lambda (n) (,g (funcall ,f ,f ,g) n)))
 (lambda (f n) (if (zerop n) 1 (* n (funcall f (1- n)))))
 5)

We have here lines 1-4.  Lines 2-4 are the three arguments of the
lambda expression in line 1.  Line 3 is the actual non-recursive
function definition which gets a function it will call for recursion,
and the numeric argument.  Since the called function does _not_ get a
function argument, we can't feed the function itself into the
function argument of the function.  Instead we are fabricating a
function to pass in line 2.  The fabricated function just gets the
numeric argument.  The fabricator function is called f, and it gets
passed itself, and the not-yet-recursive function.  It fabricates the
recursive version of the function from being passed itself and the
not-yet-recursive function, and builds the recursive version of the
function from the not-yet-recursive function and the fabricated
recursive version of it.  Which is only possible since it does not
actually call the finished function before it is finished, although
it already has a name for it it uses in its definition.  This
fabricated recursive function is then called in line 1.

I have to admit that it took me hours to come up with that thing.  It
is pretty clean in that it does not rely on any dynamic binding and
will transfer straight to Scheme.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum
Email: David.Kastrup@t-online.de


reply via email to

[Prev in Thread] Current Thread [Next in Thread]