From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: pjb@informatimago.com (Pascal J. Bourguignon) Newsgroups: gmane.emacs.help Subject: Re: lambda inside a let or letrec Date: Thu, 10 Jun 2010 16:28:02 +0200 Organization: Informatimago Message-ID: References: <6b121cee-8a28-4e27-a733-40d22028e05d@i28g2000yqa.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1291831082 7763 80.91.229.12 (8 Dec 2010 17:58:02 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Wed, 8 Dec 2010 17:58:02 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Dec 08 18:57:56 2010 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1PQOH5-0001vz-GL for geh-help-gnu-emacs@m.gmane.org; Wed, 08 Dec 2010 18:57:55 +0100 Original-Received: from localhost ([127.0.0.1]:41226 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PQOH5-0001to-1i for geh-help-gnu-emacs@m.gmane.org; Wed, 08 Dec 2010 12:57:55 -0500 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help,comp.lang.lisp,comp.lang.scheme Original-Lines: 98 Original-X-Trace: individual.net s7JI5dO3Em8YEZ/FCPShmA9cUJI5pjcHURzLFBR92wn6jfaDpr Cancel-Lock: sha1:YTE3Mzg3OWVkNjllY2I5NjRhODVkNGZhZTZlOWU5NDk2ZjkyOWViYw== sha1:K885/A+cVK4C8/hWZGSpCGnSThk= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.101 (Gnus v5.10.10) Emacs/23.1 (darwin) Original-Xref: usenet.stanford.edu gnu.emacs.help:178799 comp.lang.lisp:288900 comp.lang.scheme:86934 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:75744 Archived-At: bolega writes: > My apologies in advance to comp.lang.scheme and comp.lang.lisp. > > I am trying to run a certain syntax inside emacs lisp. > > I know basically how let works > > (let (list of pairs of var value) (function)) > > This is like a lambda function call , only the order is different. > > But the novelty i saw reading a book on common lisp or scheme is this > and i failed to run in emacs. plz tell what modifications are needed > and i know they are different. > > ( (lambda (n) (+ 1 n)) 3) ;;; works in emacs > > (let > ((a (lambda (n) (+ 1 n))) (b 3)) (a b)) ;;; does NOT work in emacs > > basically we are trying to use / abuse the let in that in the pair we > define a equal to a lambda. Then another pair where a value of b is > defined. > > next, we want a to operate on b. > > Why does it fail ? > > The scheme/lisp book/paper where it was seen (forgot) used letrec. > > Can someone enlighten me how set! and let can be used to formulate > recursion when the let has no recursion built in it ? As indicated by David, it's because scheme (and eg. LeLisp) is a Lisp-1 while emacs lisp and Common Lisp are Lisp-2. In a Lisp-1 there's a single slot for both values and functions, functions are just values like others, and a symbol has only one value, which may be a function or something else. In this case, both LET and the function application refer the same unique value of the symbol. This has the advantage of being simple conceptually, and to be able to write with a simplier syntax function code of higher order. In a Lisp-2 there are two slots, one for a value (which may be a function too) and one for a function (which may only be unbound or a function). In this case, depending on the context, the value slot XOR the function slot is used. LET binds the value slot, the symbol by itself evaluates to the contents of its value slots (when it's a special variable). On the other hand DEFUN and function application refer the function slot. This has the advantage of reducing a lot of collision problems in writing unhygienic macros, and to offer a double meaning that matches more closely natural languages. In a Lisp-2 you can write: (let ((buffalo (get-a-bull))) (flet ((buffalo (what) (do-it what))) (buffalo buffalo))) and have (do-it (get-a-bull)) executed. On the other hand, if you want to use a value as a function, you have to go thru APPLY or FUNCALL: (let ((fun (lambda (x) (be-happy-with x)))) (funcall fun 'foot)) In a Lisp-1 you would have to write: (let ((buffalo-as-noun (get-a-bull)) (buffalo-as-verb (lambda (what) (do-it what)))) (buffalo-as-verb buffalo-as-noon)) Now, IIRC, Pascal Costanza has published in cll a few years ago a macro that will allow you to write Lisp-1 function applications in a Lisp-2, so that when you need locally to call a lot of functions stored in value slots you may write it like in scheme. Try to groups.google cll for Pascal Costanza Lisp-1 macro. -- __Pascal Bourguignon__ http://www.informatimago.com