From: charlie
Subject: Closures in Common Lisp
Date: 
Message-ID: <1189687807.902226.20720@w3g2000hsg.googlegroups.com>
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_%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)))

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

From: Frank Buss
Subject: Re: Closures in Common Lisp
Date: 
Message-ID: <1vefxlc74zsqo$.n7f1rbd5uzl7.dlg@40tude.net>
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, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: fortunatus
Subject: Re: Closures in Common Lisp
Date: 
Message-ID: <1189690673.549030.252080@w3g2000hsg.googlegroups.com>
> 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...
From: Kent M Pitman
Subject: Re: Closures in Common Lisp
Date: 
Message-ID: <uzlzqh8oz.fsf@nhplace.com>
fortunatus <··············@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/Technical-Issues.html

(It's a cleaned up form of a longer document we submitted to X3J13 
 as part of the CL design process.)
From: Rainer Joswig
Subject: Re: Closures in Common Lisp
Date: 
Message-ID: <joswig-505697.15400913092007@news-europe.giganews.com>
In article <·······················@w3g2000hsg.googlegroups.com>,
 charlie <·········@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_%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
From: charlie
Subject: Re: Closures in Common Lisp
Date: 
Message-ID: <1189691831.220304.68110@r29g2000hsg.googlegroups.com>
On Sep 13, 6:40 pm, Rainer Joswig <······@lisp.de> wrote:
> In article <·······················@w3g2000hsg.googlegroups.com>,
>
>
>
>  charlie <·········@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