all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tassilo Horn <tassilo@member.fsf.org>
To: help-gnu-emacs@gnu.org
Subject: Re: The Paul Graham's "Revenge of the nerds" cummulator function and the solution in Emacs Lisp
Date: Tue, 09 Jun 2009 08:40:59 +0200	[thread overview]
Message-ID: <871vptzz7o.fsf@thinkpad.tsdh.de> (raw)
In-Reply-To: 87my8izafy.fsf@cii.fc.ul.pt

Tiago Charters de Azevedo <tca@cii.fc.ul.pt> writes:

Hi Tiago,

> Should not the function foo in Common Lisp work with Emacs Lisp?
>
> (defun foo (n)
>   (lambda (i)     
>     (incf n i))) 

No, that won't work because emacs has no lexical scoping and that code
is a closure.  See the elisp manual:

,----[ (info "(elisp)Extent") ]
|    To illustrate this, the function below, `make-add', returns a
| function that purports to add N to its own argument M.  This would work
| in Common Lisp, but it does not do the job in Emacs Lisp, because after
| the call to `make-add' exits, the variable `n' is no longer bound to
| the actual argument 2.
| 
|      (defun make-add (n)
|          (function (lambda (m) (+ n m))))  ; Return a function.
|           => make-add
|      (fset 'add2 (make-add 2))  ; Define function `add2'
|                                 ;   with `(make-add 2)'.
|           => (lambda (m) (+ n m))
|      (add2 4)                   ; Try to add 2 to 4.
|      error--> Symbol's value as variable is void: n
| 
|    Some Lisp dialects have "closures," objects that are like functions
| but record additional variable bindings.  Emacs Lisp does not have
| closures.
`----

Bye,
Tassilo
-- 
Chuck Norris once rode a bull, and nine months later it had a calf. 





  reply	other threads:[~2009-06-09  6:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-08 21:23 The Paul Graham's "Revenge of the nerds" cummulator function and the solution in Emacs Lisp Tiago Charters de Azevedo
2009-06-09  6:40 ` Tassilo Horn [this message]
     [not found] <mailman.243.1244499530.2239.help-gnu-emacs@gnu.org>
2009-06-09  0:27 ` Barry Margolin

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=871vptzz7o.fsf@thinkpad.tsdh.de \
    --to=tassilo@member.fsf.org \
    --cc=help-gnu-emacs@gnu.org \
    /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.