unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* ** Graded examples of lambda functions in emacs lisp, how to create hook variable? **
@ 2002-10-07 15:38 gnuist
  2002-10-07 15:54 ` David Kastrup
  2002-10-07 17:48 ` Kevin Rodgers
  0 siblings, 2 replies; 8+ messages in thread
From: gnuist @ 2002-10-07 15:38 UTC (permalink / raw)


lambda is often used to associate a hook with
the anonymous function. I do not see what advantage this gives over defun
other than saving a name in the name-space. Is there any other advantage
of lambda? Or is defun defined using lambda and name associating function?
The Lisp papers talk of "LABEL" function. But where is it in emacs or what
is its emacs counterpart called?

Here is a lambda function that I know for starters.

( (lambda(x y) (- x y))  1 2)

C-x C-e gives -1 in the minibuffer.

I can write more complicated defuns, single recursion, gcd, and all classic
stuff. But I am looking for a particularly instructive and clear example
of a double recursion and then probably a tricky one.

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"?

One last question at this stage: I know how you "add-hook" but how do you
create a hook variable in the first place? Is it something particular to
emacs?

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: ** Graded examples of lambda functions in emacs lisp, how to create hook variable? **
  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-07 17:48 ` Kevin Rodgers
  1 sibling, 1 reply; 8+ messages in thread
From: David Kastrup @ 2002-10-07 15:54 UTC (permalink / raw)


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)

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: ** Graded examples of lambda functions in emacs lisp, how to create hook variable? **
  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-07 17:48 ` Kevin Rodgers
  2002-10-08 14:28   ` gnuist006
  1 sibling, 1 reply; 8+ messages in thread
From: Kevin Rodgers @ 2002-10-07 17:48 UTC (permalink / raw)


gnuist wrote:

> One last question at this stage: I know how you "add-hook" but how do you
> create a hook variable in the first place? Is it something particular to
> emacs?


(defvar some-hook nil)


(defun some-function (...)
   ...
   (run-hooks 'some-hook)
   ...)

-- 
<a href="mailto:&lt;kevinr&#64;ihs.com&gt;">Kevin Rodgers</a>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: ** Graded examples of lambda functions in emacs lisp, how to create hook variable? **
  2002-10-07 15:54 ` David Kastrup
@ 2002-10-08 14:21   ` gnuist006
  2002-10-08 15:13     ` David Kastrup
  0 siblings, 1 reply; 8+ messages in thread
From: gnuist006 @ 2002-10-08 14:21 UTC (permalink / raw)


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.

However, I would want some explanatory comments.

Is funcall a primitive function of emacs lisp in the
sense of the 5 primitive functions of JM (macarthy's) lisp?
Is it corresponding to the primitive "LABEL" that is
discussed in his papers?

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.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: ** Graded examples of lambda functions in emacs lisp, how to create hook variable? **
  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
  0 siblings, 2 replies; 8+ messages in thread
From: gnuist006 @ 2002-10-08 14:28 UTC (permalink / raw)


Kevin Rodgers <kevinr@ihs.com> wrote in message news:<3DA1C8EC.6070801@ihs.com>...
> gnuist wrote:
> 
> > One last question at this stage: I know how you "add-hook" but how do you
> > create a hook variable in the first place? Is it something particular to
> > emacs?
> 
> 
> (defvar some-hook nil)
> 
> 
> (defun some-function (...)
>    ...
>    (run-hooks 'some-hook)
>    ...)

defvar creates a hook variable.
run-hooks associates some-hook with the some-function.

Now one writes lambda functions associated with the some-hook
without modifying the some-function????

Is that the idea? The post was incomplete. Perhaps the author
know too much for us. But I want a simple concrete minimal example
of running code.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: ** Graded examples of lambda functions in emacs lisp, how to create hook variable? **
  2002-10-08 14:21   ` gnuist006
@ 2002-10-08 15:13     ` David Kastrup
  0 siblings, 0 replies; 8+ messages in thread
From: David Kastrup @ 2002-10-08 15:13 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: ** Graded examples of lambda functions in emacs lisp, how to create hook variable? **
  2002-10-08 14:28   ` gnuist006
@ 2002-10-08 16:16     ` Mark A. Flacy
  2002-10-08 17:19     ` Kevin Rodgers
  1 sibling, 0 replies; 8+ messages in thread
From: Mark A. Flacy @ 2002-10-08 16:16 UTC (permalink / raw)


>>>>> "gnuist006" == gnuist006  <gnuist006@hotmail.com> writes:
gnuist006> 
gnuist006> Kevin Rodgers <kevinr@ihs.com> wrote in message news:<3DA1C8EC.6070801@ihs.com>...
>> gnuist wrote:
>> 
>> > One last question at this stage: I know how you "add-hook" but how do you
>> > create a hook variable in the first place? Is it something particular to
>> > emacs?
>> 
>> 
>> (defvar some-hook nil)
>> 
>> 
>> (defun some-function (...)
>> ...
>> (run-hooks 'some-hook)
>> ...)
gnuist006> 
gnuist006> defvar creates a hook variable.
gnuist006> run-hooks associates some-hook with the some-function.
gnuist006> 
gnuist006> Now one writes lambda functions associated with the some-hook
gnuist006> without modifying the some-function????
gnuist006> 
gnuist006> Is that the idea? The post was incomplete. Perhaps the author
gnuist006> know too much for us. But I want a simple concrete minimal example
gnuist006> of running code.

One would expect you to work a little harder at this.

What does "C-h f run-hooks <RETURN>" tell you?

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: ** Graded examples of lambda functions in emacs lisp, how to create hook variable? **
  2002-10-08 14:28   ` gnuist006
  2002-10-08 16:16     ` Mark A. Flacy
@ 2002-10-08 17:19     ` Kevin Rodgers
  1 sibling, 0 replies; 8+ messages in thread
From: Kevin Rodgers @ 2002-10-08 17:19 UTC (permalink / raw)


gnuist006 wrote:

> Kevin Rodgers <kevinr@ihs.com> wrote in message news:<3DA1C8EC.6070801@ihs.com>...
> 
>>gnuist wrote:
>>
>>>One last question at this stage: I know how you "add-hook" but how do you
>>>create a hook variable in the first place? Is it something particular to
>>>emacs?


I should have responded: yes, it is particular to Emacs Lisp.


>>(defvar some-hook nil)
>>
>>(defun some-function (...)
>>   ...
>>   (run-hooks 'some-hook)
>>   ...)
>>
> 
> defvar creates a hook variable.
> run-hooks associates some-hook with the some-function.
> 
> Now one writes lambda functions associated with the some-hook
> without modifying the some-function????
> 
> Is that the idea? The post was incomplete. Perhaps the author
> know too much for us. But I want a simple concrete minimal example
> of running code.


Yes, that is the idea.  For concrete examples, simply grep (search) for
run-hooks and run-hook-with-args in the lisp/*.el and lisp/*/*.el files

of your Emacs distribution.


Then check out advice.el :-)

-- 
<a href="mailto:&lt;kevinr&#64;ihs.com&gt;">Kevin Rodgers</a>

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2002-10-08 17:19 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).