From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Barry Margolin Newsgroups: gmane.emacs.help 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 Organization: A noiseless patient Spider Message-ID: References: NNTP-Posting-Host: lo.gmane.org X-Trace: ger.gmane.org 1244508055 4109 80.91.229.12 (9 Jun 2009 00:40:55 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 9 Jun 2009 00:40:55 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Jun 09 02:40:52 2009 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.50) id 1MDpOS-0003f9-Sy for geh-help-gnu-emacs@m.gmane.org; Tue, 09 Jun 2009 02:40:49 +0200 Original-Received: from localhost ([127.0.0.1]:52701 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MDpOS-0003oe-4o for geh-help-gnu-emacs@m.gmane.org; Mon, 08 Jun 2009 20:40:48 -0400 Original-Path: news.stanford.edu!headwall.stanford.edu!news.glorb.com!news2.glorb.com!newsfeed.kpn.net!pfeed08.wxs.nl!feeder.news-service.com!feeder.motzarella.org!news.motzarella.org!motzarella.org!news.eternal-september.org!barmar.motzarella.org!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 64 Original-X-Trace: news.eternal-september.org U2FsdGVkX18+305DKGwQM/A/DLYbXnH9w0rm2Chz6Od+aRv1mgx3eRsNPs+qE6qSaq98maZQ91r23jyly72yP931S8eiUG2LEEU3Kkcur7sR9v2Y4rDGszdvdcbynjVeDWcmShttdG8= Original-X-Complaints-To: abuse@eternal-september.org Original-NNTP-Posting-Date: Tue, 9 Jun 2009 00:27:41 +0000 (UTC) X-Auth-Sender: U2FsdGVkX18l1yDItSiY223IFLlirD5IwpYVm/yUpAw= Cancel-Lock: sha1:tCXJCU+IkWDNgMCEUod9DXRJl+8= User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) Mail-Copies-To: nobody Original-Xref: news.stanford.edu gnu.emacs.help:169838 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:65069 Archived-At: In article , Tiago Charters de Azevedo 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 ***