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: Functions returning functions in Emacs Lisp Date: Wed, 16 Apr 2008 00:33:49 -0400 Message-ID: References: NNTP-Posting-Host: lo.gmane.org X-Trace: ger.gmane.org 1208325970 7901 80.91.229.12 (16 Apr 2008 06:06:10 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 16 Apr 2008 06:06:10 +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 Apr 16 08:06:39 2008 connect(): Connection refused 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 1JlzT3-0003Iq-Vy for geh-help-gnu-emacs@m.gmane.org; Wed, 16 Apr 2008 06:41:58 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JlzSP-0008Vu-Gl for geh-help-gnu-emacs@m.gmane.org; Wed, 16 Apr 2008 00:41:17 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!postnews.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local02.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail Original-NNTP-Posting-Date: Tue, 15 Apr 2008 23:33:49 -0500 Original-Newsgroups: gnu.emacs.help Mail-Copies-To: nobody User-Agent: MT-NewsWatcher/3.5.3b2 (Intel Mac OS X) Original-Lines: 57 X-Usenet-Provider: http://www.giganews.com Original-NNTP-Posting-Host: 24.34.108.171 Original-X-Trace: sv3-G8jdItm0lphulTS05dS8KnXfA05lTWsOmjzZT0DFv5E9UzWAGXK7AHXjFAXHc393SwsDmGqlPbiHX0w!2zuMirpxIcoKrYdjnLWwUO8yyauUbCMKs871Rj9Aet/k709qacQteaqF0ofygcPoC+nv/gaXQ+x5!mOYbjUwEEYNuGNkHSs9t+8JfR3rR0kIzBKwxt/4= Original-X-Complaints-To: abuse@comcast.net X-DMCA-Complaints-To: dmca@comcast.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.38 Original-Xref: shelby.stanford.edu gnu.emacs.help:157957 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:53323 Archived-At: In article , srinik001@hotmail.com wrote: > Hi, > > I am trying to learn Emacs Lisp. I know a bit of (Common) Lisp and was > trying the following on Emacs. This is to define a function called > derivative, which returns as its result a function which is the > derivative of the argument function fn, when the numerical calculation > is done over delta. So I did the following. > > (defun derivative (fn delta) > #'(lambda(x) (/ (- (funcall fn (+ x delta)) (funcall fn x)) delta))) > > --> this seemed to work (i.e. no error on C-x C-e) > > (setq c (derivative #'sin 0.001)) > > --> this seemed to work (again, no error on evaluation) > > (funcall c (/ 3.1415 2)) > > --> this threw up an error. The error is the following: > > Debugger entered--Lisp error: (void-variable fn) > (funcall fn (+ x delta)) > (- (funcall fn (+ x delta)) (funcall fn x)) > (/ (- (funcall fn ...) (funcall fn x)) delta) > (lambda (x) (/ (- ... ...) delta))(1.57075) > funcall((lambda (x) (/ (- ... ...) delta)) 1.57075) > eval((funcall c (/ 3.1415 2))) > eval-last-sexp-1(nil) > eval-last-sexp(nil) > call-interactively(eval-last-sexp) > > I tried lambda expressions on mapcar, and it seemed to work on Emacs > the way it does in Common Lisp. Could someone please tell me if I am > doing something wrong vis-a-vis Emacs, or if Emacs does not support > this? Thanks. Emacs Lisp uses dynamic scoping, not lexical scoping, so it doesn't create lexical closures. You can get the effect using lexical-let: (defun derivative (fn delta) (lexical-let ((fn fn) (delta delta)) #'(lambda (x) (/ (- (funcall fn (+ x delta)) (funcall fn x)) delta)))) -- 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 ***