[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: The Paul Graham's "Revenge of the nerds" cummulator function and the
From: |
Tassilo Horn |
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 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.0.94 (gnu/linux) |
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.