From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tim X Newsgroups: gmane.emacs.help Subject: Re: Functions returning functions in Emacs Lisp Date: Wed, 16 Apr 2008 19:05:04 +1000 Organization: Rapt Technologies Message-ID: <87ej961873.fsf@lion.rapttech.com.au> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1208338958 17736 80.91.229.12 (16 Apr 2008 09:42:38 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 16 Apr 2008 09:42:38 +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 11:43:08 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 1Jm49l-00066p-1o for geh-help-gnu-emacs@m.gmane.org; Wed, 16 Apr 2008 11:42:21 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Jm496-0007V6-81 for geh-help-gnu-emacs@m.gmane.org; Wed, 16 Apr 2008 05:41:40 -0400 Original-Path: shelby.stanford.edu!headwall.stanford.edu!newshub.sdsu.edu!border1.nntp.dca.giganews.com!nntp.giganews.com!local02.nntp.dca.giganews.com!nntp.internode!news.internode.POSTED!not-for-mail Original-NNTP-Posting-Date: Wed, 16 Apr 2008 04:05:04 -0500 Original-Newsgroups: gnu.emacs.help User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux) Cancel-Lock: sha1:mMgAYbT5iBcRQSJtqtF8v4FLqtY= Original-Lines: 77 X-Usenet-Provider: http://www.giganews.com Original-NNTP-Posting-Host: 121.44.24.120 Original-X-Trace: sv3-C2nVIXi3LnzNNB4om1M1Tiq0l3twQQBSqS0ZBivZDKURuobwpQpG6Kfw74lRwCt8l1qBT9OpaK4REtI!JJm9dPkROkBLsXGhfcHpFoNbXAwLFeLhJ9EH3OOlYV5QQ0+tjnGIZhCSOiQVIzeiwm0XiUUNfg== 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.39 Original-Xref: shelby.stanford.edu gnu.emacs.help:157962 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:53328 Archived-At: srinik001@hotmail.com writes: > On Apr 15, 9:33 pm, Barry Margolin wrote: >> In article >> , >> >> >> >> srinik...@hotmail.com wrote: >> > Hi, >> >> > I am trying to learnEmacsLisp. I know a bit of (Common) Lisp and was >> > trying the following onEmacs. 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 onEmacs >> > the way it does in Common Lisp. Could someone please tell me if I am >> > doing something wrong vis-a-visEmacs, or ifEmacsdoes not support >> > this? Thanks. >> >> EmacsLisp 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, bar...@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 *** > > Thanks for the reply. I noticed that lexical-let requires a common > lisp extension. I will google for it and figure out how to install > it. > > Regards, > Its already there, just do a (require 'cl) Tim -- tcross (at) rapttech dot com dot au