This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Exercise from Grahams "Ansi Common Lisp"

4 posts · 5241 views

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:
(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"

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"

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"

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:
(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
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p