all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: David Kastrup <David.Kastrup@t-online.de>
Subject: Re: ** Graded examples of lambda functions in emacs lisp, how to create hook variable? **
Date: 08 Oct 2002 17:13:24 +0200	[thread overview]
Message-ID: <x5vg4dyne3.fsf@tupik.goethe.zz> (raw)
In-Reply-To: b00bb831.0210080621.47f41fe3@posting.google.com

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	other threads:[~2002-10-08 15:13 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-07 15:38 ** Graded examples of lambda functions in emacs lisp, how to create hook variable? ** gnuist
2002-10-07 15:54 ` David Kastrup
2002-10-08 14:21   ` gnuist006
2002-10-08 15:13     ` David Kastrup [this message]
2002-10-07 17:48 ` Kevin Rodgers
2002-10-08 14:28   ` gnuist006
2002-10-08 16:16     ` Mark A. Flacy
2002-10-08 17:19     ` Kevin Rodgers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=x5vg4dyne3.fsf@tupik.goethe.zz \
    --to=david.kastrup@t-online.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.