Newsgroups: comp.lang.lisp
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!oitnews.harvard.edu!news.dfci.harvard.edu!camelot.ccs.neu.edu!chaos.dac.neu.edu!usenet.eel.ufl.edu!news.mathworks.com!tank.news.pipex.net!pipex!oleane!jussieu.fr!univ-lyon1.fr!in2p3.fr!swidir.switch.ch!scsing.switch.ch!news.rccn.net!master.di.fc.ul.pt!news.inesc.pt!snail!pcr
From: pcr@snail (Paulo Cesar Reis)
Subject: Re: Help
Message-ID: <DFKx3r.75z@inesc.pt>
Sender: usenet@inesc.pt
Nntp-Posting-Host: snail.inesc.pt
Organization: INESC - Inst. Eng. Sistemas e Computadores, LISBOA. PORTUGAL.
X-Newsreader: TIN [version 1.2 PL2]
References: <445t0j$skl@umbc9.umbc.edu>
Date: Wed, 27 Sep 1995 19:35:51 GMT
Lines: 120

Dimitry Shuster (dshust1@umbc.edu) wrote:
: Can someone tell me what is wrong with this code?  I have been 
: working at it for 9 hours and I just cant get it.  I feel like 
: i am going to throw this computer out the window!!!

: please help

: what I am trying to do is count the depth of a list!!!

: (defun depth (S1)
:  (cond
:   ((null S1) 0)
:   ((listp (car S1) 
:    (+ 1(depth(car S1)))))
:  (t (depth (cdr S1)))))

: I am off by one always and I have no clue how to fix it!!!!!

: Thanks to anyone who can tell me whats wrong!!!!

Hello Dimitri, isn't Lisp beautiful? Do you know the meaning of
Lisp? Lots of Stupids and Irritating Parenthesis!

Well, the problem with your function is that it only calculates the depth of
the first sublist (if any). You must remember that there may exist others.
For example, for the list ((a) (a (b))) the result of your function is 2
and not 3. The problem is the recursion, always the recursion. I saw the answer
from Peter that is an iterative solution. Now, I'll give you a possible
purely recursive one. "Programming in Lisp is a kind of art".

------------------- code -------------------
; iterative function from Peter
(defun depth2 (s)
   (cond ((null s) 0)
	 ((atom s) 0)
	 ((listp s)
	  (1+ (apply #'max (mapcar #'depth2 s))))
	 (t 'error)))

; (my) recursive function
(defun rec-depth (s1)	
   (cond ((null s1) 0)
	 ((atom s1) 0)
         (t (max (1+ (rec-depth (car s1))) 
                 (rec-depth (cdr s1))))))
--------------------------------------------

Pay attention to the recursive call in the end of my function.

And now, just for fun, here it is a "small" comparison between the two
functions, running on a Sparc Station, on Allegro Cl 4.1. As you will
notice, after compiling the two functions, the recursive one proves to be more
efficient.

----------------- the result of the tests ----------------
dribbling to file "/mnt/users/pcr/Inesc/jspell/values.txt"
 
NIL
USER(2): (load "teste.lsp")
; Loading /mnt/users/pcr/Inesc/jspell/teste.lsp.
T
USER(3): (setf a '(a (b) (a (b (c (d e))))))
(A (B) (A (B (C (D E)))))
USER(4): (depth2 a)
5
USER(5): (rec-depth a)
5
USER(6): (time (depth2 a))
cpu time (non-gc) 0 msec user, 0 msec system
cpu time (gc)     0 msec user, 0 msec system
cpu time (total)  0 msec user, 0 msec system
real time  8 msec
space allocation:
 139 cons cells, 0 symbols, 448 other bytes,
5
USER(7): (time (rec-depth a))
cpu time (non-gc) 0 msec user, 0 msec system
cpu time (gc)     0 msec user, 0 msec system
cpu time (total)  0 msec user, 0 msec system
real time  11 msec
space allocation:
 241 cons cells, 0 symbols, 832 other bytes,
5
USER(8): (compile 'depth2)
DEPTH2
NIL
NIL
USER(9): (compile 'rec-depth)
REC-DEPTH
NIL
NIL
USER(10): (time (depth2 a))
cpu time (non-gc) 0 msec user, 0 msec system
cpu time (gc)     0 msec user, 0 msec system
cpu time (total)  0 msec user, 0 msec system
real time  1 msec
space allocation:
 19 cons cells, 0 symbols, 32 other bytes,
5
USER(11): (time (rec-depth a))
cpu time (non-gc) 0 msec user, 0 msec system
cpu time (gc)     0 msec user, 0 msec system
cpu time (total)  0 msec user, 0 msec system
real time  1 msec
space allocation:
 1 cons cell, 0 symbols, 32 other bytes,
5
USER(12): (dribble)
--------------------------------------------

Best Regards and have fun with Lisp,

     Paulo Reis.

-------------------------------------------------------------
Paulo Cesar Sobral dos Reis
Instituto de Engenharia de Sistemas e Computadores
Rua Alves Redol, no 9  Lisboa - Portugal
Tel: 3100000    ext: 305
------------------------------------------------------------
