Land of Lisp recursion understanding problem
Land of Lisp recursion understanding problem
Hi Im new to LISP and am using CLISP on win 7.And trying to kearn from "The Land of Lisp"
What Im having a problem with is this
(labels ( (a (n)
(+ n 5))
(b (n)
( + (a n) 6)))
(b 10))
giving the answer 21.
Is "a" in this case a function that just adds 5 .
when passing 10 into function does it go into the n of (a n) and then gets 6 added to it. and why is (a n) brakected.
I think im almost getting it but would appreciate it very muchly if some could briefly annotate the lines above or give me some other clues
Very much appreciated
from sunny Tasmania
What Im having a problem with is this
(labels ( (a (n)
(+ n 5))
(b (n)
( + (a n) 6)))
(b 10))
giving the answer 21.
Is "a" in this case a function that just adds 5 .
when passing 10 into function does it go into the n of (a n) and then gets 6 added to it. and why is (a n) brakected.
I think im almost getting it but would appreciate it very muchly if some could briefly annotate the lines above or give me some other clues
Very much appreciated
from sunny Tasmania
Re: Land of Lisp recursion understanding problem
Recursion is when a function is defined directly or indirectly in terms of itself. In the given example no such thing occurs. Use code tags to maintain formatting. Using standard Common Lisp (note that "Lisp" is usually spelled "Lisp", with noncapital letters) formatting the code sample appears as:
LABELS is a special operator, which was probably explained in the book somewhere around the example. It defines local functions which are visible to themselves and each other (in which it is different than FLET). This particular example defines two such functions, and b is defined in terms of a. The parameter n is independent between those two functions, just as it would be between two global functions.
Code: Select all
(labels ((a (n)
(+ n 5))
(b (n)
(+ (a n) 6)))
(b 10))

 Posts: 148
 Joined: Wed Jul 30, 2008 11:26 pm
Re: Land of Lisp recursion understanding problem
Does it make more sense to you written like this?
You've probably learned about LET*. LABELS is much like LET* for functions.
Code: Select all
(defun a (n)
(+ n 5))
(defun b (n)
(+ (a n) 6))
(b 10)
Re: Land of Lisp recursion understanding problem
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 EHealth 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 anynumber) 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
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 EHealth 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 anynumber) 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

 Posts: 447
 Joined: Sat Jun 28, 2008 7:49 am
 Location: Austin, TX
 Contact:
Re: Land of Lisp recursion understanding problem
(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.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 EHealth 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 anynumber) 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
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/
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/

 Posts: 447
 Joined: Sat Jun 28, 2008 7:49 am
 Location: Austin, TX
 Contact:
Re: Land of Lisp recursion understanding problem
I should have also said that Lisp is very different than other infix languages. It doesn't use parenthesis to group terms in arithmetic equations. Rather, it uses them to indicate lists and functions (because code = data in Lisp).
So, whereas in C you might write:
in Lisp you'd write:
So, whereas in C you might write:
Code: Select all
int f(int a, int b)
{
return g(42 + (a * b));
}
Code: Select all
(defun f (a b)
(g (+ 42 (* a b))))
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/
Re: Land of Lisp recursion understanding problem
OK Dave I think Im almost there . I passed 10 to a and returned 15 and its all making sense. At the moment Im using GNU CLISP 2.49 on win7 system, is there anywhere i can get list of keystrokes ie how can you delete or would you recommend another a program
Many thanks to all
Many thanks to all

 Posts: 447
 Joined: Sat Jun 28, 2008 7:49 am
 Location: Austin, TX
 Contact:
Re: Land of Lisp recursion understanding problem
If you're typing this directly into the REPL, you can always whip out an editor of some sort (e.g. notepad on Win 7), edit your code there, and then cut/paste into the REPL. That sometimes makes it easy. If you're really wanting to go hardcore, then you can setup Emacs and run Lisp as a subprocess. That will allow you to evaluate Lisp forms (whole sexprs) with one keystroke. That's what most committed Lispers do, but some wellknown people (notably Paul Graham) have gone the cut/paste route. Some of the Lisps also include some basic commandline editing (Clisp includes Gnu Readline, for instance, at least on Linux).lrj wrote:OK Dave I think Im almost there . I passed 10 to a and returned 15 and its all making sense. At the moment Im using GNU CLISP 2.49 on win7 system, is there anywhere i can get list of keystrokes ie how can you delete or would you recommend another a program
Many thanks to all
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/