I don't think I understand how it works. Could someone kindly correct me.

1. We're defining the function 'my-length' which takes one argument 'list'2. If the argument exists and is not empty, eg () or nil3. do the following: Pass '(with four objects) to the function my-length. It's recursive so then it'll pass '(four objects) to my-length until the list is empty. What I don't understand here is "(1+" I thought that (1+ n) = (+ n 1). If that's correct, the recursive function adds: '(list with four objects) + 1 '(with four objects) + 1 '(four objects) + 1 '(objects) + 1Which doesn't make sense to me. How can you add strings and numbers? I can see that that's probably not the case. That each iteration 1 gets added to make the total of 4 (number of elements) but according to the code it seems to me that it gets added to the list of strings?!

Could you please explain how it really works? Thank you for your time and patience.

I still have a problem understanding why it doesn't spit out any errors before the last stage ((1+ (1+ (1+ (1+ 0))))Before it reaches the last iteration, it seems to be adding numbers and strings (at least that's how I see it)I know that when it adds, it never reaches the members of the list because first it will call the function recursively.I understand it is something similar to:

which, by the way, doesn't work as I don't know how to switch to the code mode before 1. It can't add a number and a non-number element. That's logical and it ignores john and bill before it tries to return anything.

So coming back to the original question: why doesn't it give errors in the my-length function? My attempt of explaining it would be that it doesn't return anything until the final iteration, butthat would probably be wrong because it does return numbers like 2 and 3 before the last call of the function.

sycamorex wrote:I still have a problem understanding why it doesn't spit out any errors before the last stage ((1+ (1+ (1+ (1+ 0))))Before it reaches the last iteration, it seems to be adding numbers and strings (at least that's how I see it)

It's not adding numbers and strings. When it calls (1+ (my-length '(with four symbols))), it's adding 1 to whatever (my-length '(with four symbols)) returns. That's not a string, it's a number. It calls (my-length '(four symbols)), which calls (my-length '(symbols)), which calls (my-length '()), which returns 0, so (1+ (my-length '()) == (1+ 0) == 1 is the length of (objects). And (1+ (my-length '(symbols)) == (1+ 1) == 2 is the length of (four symbols), and so on back up the call stack.

(There are no strings involved, anyway: '(list with four symbols) is—as it says—a list of symbols, not strings!)

Paul wrote:It's not adding numbers and strings. When it calls (1+ (my-length '(with four symbols))), it's adding 1 to whatever (my-length '(with four symbols)) returns. That's not a string, it's a number. It calls (my-length '(four symbols)), which calls (my-length '(symbols)), which calls (my-length '()), which returns 0, so (1+ (my-length '()) == (1+ 0) == 1 is the length of (objects). And (1+ (my-length '(symbols)) == (1+ 1) == 2 is the length of (four symbols), and so on back up the call stack.

(There are no strings involved, anyway: '(list with four symbols) is—as it says—a list of symbols, not strings!)

Thank you. It's getting clearer and clearer, although I can't say I understand it 100%. I'm

Could anyone kindly provide another simple example illustrating the use of recursion (and/or the (1+...) construct)?

The key point that helped me understand recursion is not to try to follow the recursion.If you recur on a list, you (well, basically) only have two cases to look at:- The list you get is emptyIf it is, you simply return the 'neutral' or final element to the operation you want to perform: In your first example,you wanted to sum up numbers, so you pass zero in the end. Here we construct a list from the elements,so we pass nil at last.- The list is not emptyThen you perform the operation you want to perform using the value of the applicationof your function to the 'cdr of the list. That can get as complicated as you like.Here, we want to construct a list, so the operation is 'cons. (By the way we use '1+ to add 1 to the current car of the list.)

Would it be too much if I asked you to give me a couple of tasks that would test my understanding of recursion?I thought it'd be better if you guys set a task for me, because as I am a LISP newbie, unknowingly I might come up with some tasks that mayrequire deeper knowledge of LISP and would be to complex for me to implement.

It can be hard to wrap one's mind around some recursive functions,and since you're learning from Barski (like I did and do) you'll want to havea feel for it when it comes to 'dice of doom' If you can afford it and like to improve yourself one little task after another, I'ld highly recomendthe 'little schemer'-book (and it's follower). Despite it's name, you can go through it with CL equally well.

Anyway and though I don't know anything about your general knowledge of CL, here are some tasks for you!

Find the position of an element in a list (you'll want to look at labels for this)!