I am trying to make a function that counts the number of lists within a list.
(defun test (a)
(if (equal a nil) (return-from test 0))
(if (not (listp a)) (print "case a") (return-from test (+ 0 (test (cdr a))))
(print "case b")(return-from test (+ 1 (test (cdr a)))))
)
I don't know how the debugger works so I tried some newbie debugging with print statements. The code above does not even execute. I have no idea why. The original code, which works but gives the wrong answer is as follows:
(defun test (a)
(if (equal a nil) (return-from test 0))
(if (not (listp a)) (return-from test (+ 0 (test (cdr a))))
(return-from test (+ 1 (test (cdr a)))))
)
This is returning the number of elements period, whether lists or not. I don't see where I am going wrong. Another problem too, it crashes when I call (test 1) or test on any atom. Of course, you cannot take cdr of an atom but what error checking can I do? What changes can I make to make this ridiculous function work?
return-fromexample ;-) Again,condwould saved you here.