lrj wrote:Thanks Paul and Ramareen,
I am just about to turn 59 and decided to try and reacticate a few neurons. I have in the past programmed in Basic VB Pascal C C++ Cobol and PHP. Of course I had of lisp a long time ago but never thougfht one way or another about it. Only recently while do some work on E-Health and Expert systems did it come up again. I thought cool; so here I am for the past week stuck on this piece of code which I think should be basic. What am I missing apart from a few neurons.
I can substitute (b 10) for ( b any-number) and get the right answer. I just cant see where it is happening. What and where each step occurrs?
can you explain it somehow like this i realize this is not neat nor correct
b= a + n + 6
a= n+5
and is (+ (a n) 6) the same as = a+n +6.
Much appreciated
(a n) is a function call to the function a with the value of n in function b as an argument to function a. This is a bit confusing because n is used as the parameter name for each of the two functions, but it's important to realize that n is local to each function.
So, in English, if you evaluate function b, it will return the result of evaluating function a with the same parameter value you pass b, plus 6. In math, if you expanded the value of function a into function b, then function b will return n + 5 + 6.
So let's follow Ramarren's example. When he calls (b 10), that invokes function b and binds the local value of n in b to 10. When function b executes, it starts to call the + function, but realizes that it needs to first evaluate a function call to a. The parameter to a is n, which has been bound to 10, so this is essentially the same as (a 10). Now we evaluate function a, again with the local variable named n in function a (which is different than the local variable named n in b) bound to 10. When b runs, it evaluates (+ n 5) which is the same as (+ 10 5), which returns 15. Now we're back in b with the result of function a. We continue to evaluate the + form, which is equivalent to (+ 15 6), which results in 21 being the value of (b 10).
Cheers, Dave
Slowly but surely the world is finding Lisp.
http://www.findinglisp.com/blog/