Page 1 of 1

### Exercise from Grahams "Ansi Common Lisp"

Posted: Sat Mar 18, 2017 4:36 am
Hello, I'm a LISP-Beginner and I'm working through Paul Grahams book.

There is an exercise: Define a function that takes a list as an argument and return true
if one of its elements is a list.

I tried:

Code: Select all

``````(defun my-find (lst)
(if (listp (car lst))
t
(my-find (cdr lst))))``````
But it will return true even if there is no list inside my list.
Could anyone please tell me what's wrong?
Thank You

### Re: Exercise from Grahams "Ansi Common Lisp"

Posted: Sat Mar 18, 2017 8:52 pm
In Common Lisp, NIL is an empty list, every list ends in NIL, and an empty list is NIL. '() is NIL, (CAR NIL) is NIL, (CDR NIL) is NIL, and (LISTP NIL) is T.

...

### Re: Exercise from Grahams "Ansi Common Lisp"

Posted: Sun Mar 19, 2017 12:12 am
Ok. So I have to test if there's a list except for the empty list.

Thank You.

### Re: Exercise from Grahams "Ansi Common Lisp"

Posted: Thu Mar 23, 2017 7:05 am
The reason fo that is because you have no stop condition for when the function has failed to find a sublist. the final tail of a proper list is nil and (car nil) is nil. nil is a list.

The logic must check if the list is the empty list and return nil.
if it is not then you can do the rest your function is doing and it will work.
Since this will have 3 terms I would have used cond instead of if but that is just a style suggestion.

Thus:

Code: Select all

``````
(list-element-p '())          ; ==> nil
(list-element-p '(1))         ; ==> nil
(list-element-p '((1)))       ; ==> t
(list-element-p '(nil))       ; ==> t
(list-element-p '(1 2 (3)))   ; ==> t``````