From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: srinik001@hotmail.com Newsgroups: gmane.emacs.help Subject: Re: Functions returning functions in Emacs Lisp Date: Tue, 15 Apr 2008 22:22:54 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1208327919 14288 80.91.229.12 (16 Apr 2008 06:38:39 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 16 Apr 2008 06:38:39 +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:38:59 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 1Jm1CO-00053i-IL for geh-help-gnu-emacs@m.gmane.org; Wed, 16 Apr 2008 08:32:52 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Jm1Bk-0006Qf-1q for geh-help-gnu-emacs@m.gmane.org; Wed, 16 Apr 2008 02:32:12 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!postnews.google.com!b9g2000prh.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 68 Original-NNTP-Posting-Host: 24.6.229.192 Original-X-Trace: posting.google.com 1208323374 7628 127.0.0.1 (16 Apr 2008 05:22:54 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Wed, 16 Apr 2008 05:22:54 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: b9g2000prh.googlegroups.com; posting-host=24.6.229.192; posting-account=80yX_goAAADR1ljaDcvuAw9eDZS-8Z3p User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13,gzip(gfe),gzip(gfe) Original-Xref: shelby.stanford.edu gnu.emacs.help:157959 X-Mailman-Approved-At: Wed, 16 Apr 2008 02:31:14 -0400 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:53325 Archived-At: 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, SK