question about Chapter 4 Land of Lisp p.51

44 views
Skip to first unread message

farmkitteh

unread,
Jun 15, 2012, 7:26:54 PM6/15/12
to land-o...@googlegroups.com
Hello,
I'm working my way through this wonderful book and I hope that someone can explain the function at the top of page 51 in more detail.  I don't understand what is happening with this part:  
(1+ (my-length (cdr list)))
 0))

How is this chomping off each bit until the list is empty?  How do the 1+ and 0 figure in?

Here is the complete example:
>(defun my-length (list)
    (if list
       (1+ (my-length (cdr list)))
       0))

>(my-length (list with four symbols))

4

Thanks.  I know this is really a newbie question. I think I've understood everything up until this point.  I realize that crd takes the second and subsequent items in a list and this is repeating until the list is empty but I don't understand the +1 and 0 in this context. Thanks in advance!

Purity Control

unread,
Jun 16, 2012, 2:12:18 AM6/16/12
to land-o...@googlegroups.com
I have answered this in a previous post  https://groups.google.com/forum/?fromgroups#!searchin/land-of-lisp/purity$20control/land-of-lisp/HtqlXIa6lp8/NcPjx0fcqEAJ

But here is some of the text cut and pasted for you :-

If you think of lisp expressions as ordering in the same way as 
complex mathematic expressions this may help you. Whatever a function 
returns sits where the function call is. 


(defun my-length (list) 
     (if list 
         (1+ (my-length (cdr list))) 
         0)) 
> (my-length ' (list with four symbols)) 

becomes 
  (if '(list with four variables) 
which evaluate to true so we operate on the line 
  (1+ (my-length (cdr list))) 
which becomes 
  (1+ (my-length '(with four varianbles))) 
so we get to 
  (1+ (if  '(with four variables)......)) 
which again evaluates to true so we take the first line 
  (1+ (1+ (my-length '(four variables)))) 
which becomes 
  (1+ (1+ (if '(four variables).....))) 
again true so 
  (1+ (1+ (1+ (my-length '(variables))))) 
which becomes 
  (1+ (1+ (1+ (if ('variables) ....)))) 
which again avaluates to true so 
  (1+ (1+ (1+ (1+ (my-length '()))))) 
which becomes 
  (1+ (1+ (1+ (1+ (if '() .....))))) 
this evaluates to false and so returns the second line which is zero 
  (1+ (1+ (1+ (1+ 0)))) 
  (1+ (1+ (1+ 1))) 
  (1+ (1+ 2)) 
  (1+ 3) 
  4 


Hope this makes a bit more sense. 

farmkitteh

unread,
Jun 16, 2012, 9:59:43 AM6/16/12
to land-o...@googlegroups.com
Thanks Purity Control!  I'm sorry I didn't find your original post. I'll look harder next time.  I understand now thanks to you.  I'm glad I asked because I don't think I would have figured that one out.  

Also I see that I have a typo in my post. In the sentence "I realize that crd takes ..." should of course be "I realize that cdr takes..."
Again, thanks Purity Control for your detailed explanation.

Purity Control

unread,
Jun 16, 2012, 11:45:36 AM6/16/12
to land-o...@googlegroups.com
Glad it all makes sense.
Recursion seems to be a regular stumbling block for people learning but once you get it, its like a eureka moment so stick with it even if it takes a little while.
The book is an absolutely wonderful way to learn lisp. 
I think you will have great fun going through it.
Reply all
Reply to author
Forward
0 new messages