| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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 |
|
#2
| |||
| |||
| 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 |
|
#3
| |||
| |||
| > 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... |
|
#4
| |||
| |||
| 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 |
|
#5
| |||
| |||
| 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 |
|
#6
| |||
| |||
| 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.) |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.