From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: David Kastrup Newsgroups: gmane.emacs.help Subject: Re: ** Graded examples of lambda functions in emacs lisp, how to create hook variable? ** Date: 08 Oct 2002 17:13:24 +0200 Organization: T-Online Sender: help-gnu-emacs-admin@gnu.org Message-ID: References: <9e8ebeb2.0210070738.1b12118d@posting.google.com> NNTP-Posting-Host: localhost.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: main.gmane.org 1034090519 30116 127.0.0.1 (8 Oct 2002 15:21:59 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Tue, 8 Oct 2002 15:21:59 +0000 (UTC) Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 17ywBH-0007pJ-00 for ; Tue, 08 Oct 2002 17:21:55 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.10) id 17ywAb-0001iB-00; Tue, 08 Oct 2002 11:21:13 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!syros.belnet.be!news.belnet.be!colt.net!peernews3.colt.net!newsfeed00.sul.t-online.de!newsmm00.sul.t-online.com!t-online.de!news.t-online.com!not-for-mail Original-Newsgroups: gnu.emacs.help,comp.emacs Original-Lines: 92 Original-X-Trace: news.t-online.com 1034090006 00 6130 Jsa2byfSS9Yplu 021008 15:13:26 Original-X-Complaints-To: abuse@t-online.com X-Sender: 520018396234-0001@t-dialin.net X-Face: 2FEFf>]>q>2iw=B6,xrUubRI>pR&Ml9=ao@P@i)L:\urd*t9M~y1^:+Y]'C0~{mAl`oQuAl \!3KEIp?*w`|bL5qr,H)LFO6Q=qx~iH4DN;i";/yuIsqbLLCh/!U#X[S~(5eZ41to5f%E@'ELIi$t^ Vc\LWP@J5p^rst0+('>Er0=^1{]M9!p?&:\z]|;&=NP3AhB!B_bi^]Pfkw User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50 Original-Xref: shelby.stanford.edu gnu.emacs.help:105838 comp.emacs:75137 Original-To: help-gnu-emacs@gnu.org Errors-To: help-gnu-emacs-admin@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.0.11 Precedence: bulk List-Help: List-Post: List-Subscribe: , List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: Xref: main.gmane.org gmane.emacs.help:2385 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:2385 gnuist006@hotmail.com (gnuist006) writes: > David Kastrup wrote in message news:... > > 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