Closures in Common Lisp

This is a discussion on Closures in Common Lisp within the lisp forums in Programming Languages category; Hello, I am fairly new to LISP and I find it a really cool language. Some concepts are a bit difficult to understand and right now I'm stuck (with understanding closures). I read this wikipedia article http://en.wikipedia.org/wiki/Closure...ter_science%29 which seemed to make sense. It gave this example (in Scheme): ; Return a function that approximates the derivative of f ; using an interval of dx, which should be appropriately small. (define (derivative f dx) (lambda (x) (/ (- (f (+ x dx)) (f x)) dx))) Since I'm using common lisp - I translated this to: (defun derivative (f dx) (lambda (x) ...

Go Back   Application Development Forum > Programming Languages > lisp

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 09-13-2007, 08:50 AM
charlie
Guest
 
Default Closures in Common Lisp

Hello,

I am fairly new to LISP and I find it a really cool language. Some
concepts are a bit difficult to understand and right now I'm stuck
(with understanding closures).

I read this wikipedia article http://en.wikipedia.org/wiki/Closure...ter_science%29
which seemed to make sense. It gave this example (in Scheme):

; Return a function that approximates the derivative of f
; using an interval of dx, which should be appropriately small.
(define (derivative f dx)
(lambda (x) (/ (- (f (+ x dx)) (f x)) dx)))

Since I'm using common lisp - I translated this to:
(defun derivative (f dx)
(lambda (x) (/ (- (f (+ x dx)) (f x)) dx)))

Then I tried it out like so:
(setq a (derivative (lambda (x) (* x x)) 0.01))
(funcall a 25)

But BLAMMO! - I got a debug error: "EVAL: undefined function F"

Now I'm confused - isn't derivative supposed to "close over" my
definition of "F"? Why is it saying undefined function?

Help.

/Vx

Reply With Quote
  #2  
Old 09-13-2007, 09:00 AM
Frank Buss
Guest
 
Default Re: Closures in Common Lisp

charlie wrote:

> Since I'm using common lisp - I translated this to:
> (defun derivative (f dx)
> (lambda (x) (/ (- (f (+ x dx)) (f x)) dx)))
>
> Then I tried it out like so:
> (setq a (derivative (lambda (x) (* x x)) 0.01))
> (funcall a 25)
>
> But BLAMMO! - I got a debug error: "EVAL: undefined function F"


Your Lisp to Scheme translation was no complete. Hint: Lisp1/Lisp2

--
Frank Buss, fb@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
Reply With Quote
  #3  
Old 09-13-2007, 09:37 AM
fortunatus
Guest
 
Default Re: Closures in Common Lisp


> Your Lisp to Scheme translation was no complete. Hint: Lisp1/Lisp2


Since "Lisp1/Lisp2" might be a really hard clue for a beginner to get
- it requires some study of history - I'll spoil it by saying there
might be some more funcalls needed...

Reply With Quote
  #4  
Old 09-13-2007, 09:40 AM
Rainer Joswig
Guest
 
Default Re: Closures in Common Lisp

In article <1189687807.902226.20720@w3g2000hsg.googlegroups.c om>,
charlie <charlievx@gmail.com> wrote:

> Hello,
>
> I am fairly new to LISP and I find it a really cool language. Some
> concepts are a bit difficult to understand and right now I'm stuck
> (with understanding closures).
>
> I read this wikipedia article http://en.wikipedia.org/wiki/Closure...ter_science%29
> which seemed to make sense. It gave this example (in Scheme):
>
> ; Return a function that approximates the derivative of f
> ; using an interval of dx, which should be appropriately small.
> (define (derivative f dx)
> (lambda (x) (/ (- (f (+ x dx)) (f x)) dx)))
>
> Since I'm using common lisp - I translated this to:
> (defun derivative (f dx)
> (lambda (x) (/ (- (f (+ x dx)) (f x)) dx)))


F is a local variable. If it points to a function, you
need FUNCALL to call that function.

(lambda (x) (/ (- (funcall f (+ x dx)) (funcall f x)) dx))

>
> Then I tried it out like so:
> (setq a (derivative (lambda (x) (* x x)) 0.01))
> (funcall a 25)
>
> But BLAMMO! - I got a debug error: "EVAL: undefined function F"
>
> Now I'm confused - isn't derivative supposed to "close over" my
> definition of "F"? Why is it saying undefined function?
>
> Help.
>
> /Vx


--
http://lispm.dyndns.org
Reply With Quote
  #5  
Old 09-13-2007, 09:57 AM
charlie
Guest
 
Default Re: Closures in Common Lisp

On Sep 13, 6:40 pm, Rainer Joswig <jos...@lisp.de> wrote:
> In article <1189687807.902226.20...@w3g2000hsg.googlegroups.c om>,
>
>
>
> charlie <charli...@gmail.com> wrote:
> > Hello,

>
> > I am fairly new to LISP and I find it a really cool language. Some
> > concepts are a bit difficult to understand and right now I'm stuck
> > (with understanding closures).

>
> > I read this wikipedia articlehttp://en.wikipedia.org/wiki/Closure_%28computer_science%29
> > which seemed to make sense. It gave this example (in Scheme):

>
> > ; Return a function that approximates the derivative of f
> > ; using an interval of dx, which should be appropriately small.
> > (define (derivative f dx)
> > (lambda (x) (/ (- (f (+ x dx)) (f x)) dx)))

>
> > Since I'm using common lisp - I translated this to:
> > (defun derivative (f dx)
> > (lambda (x) (/ (- (f (+ x dx)) (f x)) dx)))

>
> F is a local variable. If it points to a function, you
> need FUNCALL to call that function.
>
> (lambda (x) (/ (- (funcall f (+ x dx)) (funcall f x)) dx))
>
>
>
> > Then I tried it out like so:
> > (setq a (derivative (lambda (x) (* x x)) 0.01))
> > (funcall a 25)

>
> > But BLAMMO! - I got a debug error: "EVAL: undefined function F"

>
> > Now I'm confused - isn't derivative supposed to "close over" my
> > definition of "F"? Why is it saying undefined function?

>
> > Help.

>
> > /Vx

>
> --http://lispm.dyndns.org


It works!!!!

Thanks for the help guys :-)

/Vx

Reply With Quote
  #6  
Old 09-13-2007, 11:43 AM
Kent M Pitman
Guest
 
Default Re: Closures in Common Lisp

fortunatus <daniel.eliason@excite.com> writes:

> > Your Lisp to Scheme translation was no complete. Hint: Lisp1/Lisp2

>
> Since "Lisp1/Lisp2" might be a really hard clue for a beginner to get
> - it requires some study of history - I'll spoil it by saying there
> might be some more funcalls needed...


If you want the origin of this term, see this paper by Dick Gabriel and me:

http://www.nhplace.com/kent/Papers/T...al-Issues.html

(It's a cleaned up form of a longer document we submitted to X3J13
as part of the CL design process.)
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 08:54 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.