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