all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Functions returning functions in Emacs Lisp
@ 2008-04-16  3:18 srinik001
  2008-04-16  4:33 ` Barry Margolin
  0 siblings, 1 reply; 6+ messages in thread
From: srinik001 @ 2008-04-16  3:18 UTC (permalink / raw)
  To: help-gnu-emacs

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.

Regards,

SK


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Functions returning functions in Emacs Lisp
  2008-04-16  3:18 Functions returning functions in Emacs Lisp srinik001
@ 2008-04-16  4:33 ` Barry Margolin
  2008-04-16  5:22   ` srinik001
  0 siblings, 1 reply; 6+ messages in thread
From: Barry Margolin @ 2008-04-16  4:33 UTC (permalink / raw)
  To: help-gnu-emacs

In article 
<c98a949b-5ada-490a-805f-298e84783e01@l28g2000prd.googlegroups.com>,
 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 ***


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Functions returning functions in Emacs Lisp
  2008-04-16  4:33 ` Barry Margolin
@ 2008-04-16  5:22   ` srinik001
  2008-04-16  8:06     ` David Hansen
  2008-04-16  9:05     ` Tim X
  0 siblings, 2 replies; 6+ messages in thread
From: srinik001 @ 2008-04-16  5:22 UTC (permalink / raw)
  To: help-gnu-emacs

On Apr 15, 9:33 pm, Barry Margolin <bar...@alum.mit.edu> wrote:
> In article
> <c98a949b-5ada-490a-805f-298e84783...@l28g2000prd.googlegroups.com>,
>
>
>
>  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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Functions returning functions in Emacs Lisp
  2008-04-16  5:22   ` srinik001
@ 2008-04-16  8:06     ` David Hansen
  2008-04-16  9:05     ` Tim X
  1 sibling, 0 replies; 6+ messages in thread
From: David Hansen @ 2008-04-16  8:06 UTC (permalink / raw)
  To: help-gnu-emacs

On Tue, 15 Apr 2008 22:22:54 -0700 (PDT) srinik wrote:

> 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.

It comes with emacs.  Just (require 'cl) or as long as you only use
macros (eval-when-compile (require 'cl)).

David





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Functions returning functions in Emacs Lisp
  2008-04-16  5:22   ` srinik001
  2008-04-16  8:06     ` David Hansen
@ 2008-04-16  9:05     ` Tim X
  2008-04-16 13:33       ` Lennart Borgman (gmail)
  1 sibling, 1 reply; 6+ messages in thread
From: Tim X @ 2008-04-16  9:05 UTC (permalink / raw)
  To: help-gnu-emacs

srinik001@hotmail.com writes:

> On Apr 15, 9:33 pm, Barry Margolin <bar...@alum.mit.edu> wrote:
>> In article
>> <c98a949b-5ada-490a-805f-298e84783...@l28g2000prd.googlegroups.com>,
>>
>>
>>
>>  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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Functions returning functions in Emacs Lisp
  2008-04-16  9:05     ` Tim X
@ 2008-04-16 13:33       ` Lennart Borgman (gmail)
  0 siblings, 0 replies; 6+ messages in thread
From: Lennart Borgman (gmail) @ 2008-04-16 13:33 UTC (permalink / raw)
  Cc: help-gnu-emacs

> Its already there, just do a (require 'cl)


Or even better

   (eval-when-compile (require 'cl))

as elisp info says.




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2008-04-16 13:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-04-16  3:18 Functions returning functions in Emacs Lisp srinik001
2008-04-16  4:33 ` Barry Margolin
2008-04-16  5:22   ` srinik001
2008-04-16  8:06     ` David Hansen
2008-04-16  9:05     ` Tim X
2008-04-16 13:33       ` Lennart Borgman (gmail)

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.