unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Barry Margolin <barmar@alum.mit.edu>
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: Mon, 08 Jun 2009 20:27:41 -0400	[thread overview]
Message-ID: <barmar-FED93B.20274108062009@mara100-84.onlink.net> (raw)
In-Reply-To: mailman.243.1244499530.2239.help-gnu-emacs@gnu.org

In article <mailman.243.1244499530.2239.help-gnu-emacs@gnu.org>,
 Tiago Charters de Azevedo <tca@cii.fc.ul.pt> wrote:

> I've been reading  Paul Graham's "Revenge of the nerds"
> (http://www.paulgraham.com/icad.html) particularly  the appendix. It states 
> the
> problem of writing "a function that generates accumulators-- a function that 
> takes
> a number n, and returns a function that takes another number i and returns n
> incremented by i. (That's incremented by, not plus. An accumulator has to
> accumulate.)"
> 
> Should not the function foo in Common Lisp  work with Emacs Lisp?

Emacs Lisp has dynamic scoping, not lexical scoping.  If you want to 
emulate lexical scoping, (require 'cl-macs) and use lexical-let:

(defun foo (n)
  (lexical-let ((lexn n))
    (lambda (i)
      (incf lexn n))))

> 
> (defun foo (n)
>   (lambda (i)     
>     (incf n i))) 
> 
> by setting
> 
> (setq a (foo 3))
> 
> followed  by 
> 
> (funcall a 1)
> 
> When I try to evaluate (funcall a 1) in Emacs (in emacs-lisp mode) I get:
> 
> Debugger entered--Lisp error: (void-variable n)
>   (+ n i)
>   (setq n (+ n i))
>   (incf n i)
>   (lambda (i) (incf n i))(1)
>   funcall((lambda (i) (incf n i)) 1)
>   eval((funcall a 1))
>   eval-last-sexp-1(nil)
>   eval-last-sexp(nil)
>   call-interactively(eval-last-sexp
> 
> 
> It seems that emacs considers that n is a local variable because it is inside 
> a
> lambda expression?
> 
> Is this a bug? Or I'm missing something?
> 
> Thanks
> 
> tca

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***


       reply	other threads:[~2009-06-09  0:27 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.243.1244499530.2239.help-gnu-emacs@gnu.org>
2009-06-09  0:27 ` Barry Margolin [this message]
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

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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=barmar-FED93B.20274108062009@mara100-84.onlink.net \
    --to=barmar@alum.mit.edu \
    --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.
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).