Exercise from Grahams "Ansi Common Lisp"

You have problems, and we're glad to hear them. Explain the problem, what you have tried, and where you got stuck.
Feel free to share a little info on yourself and the course.
Forum rules
Please respect your teacher's guidelines. Homework is a learning tool. If we just post answers, we aren't actually helping. When you post questions, be sure to show what you have tried or what you don't understand.
Post Reply
freeside
Posts: 2
Joined: Sat Mar 18, 2017 4:16 am

Exercise from Grahams "Ansi Common Lisp"

Post by freeside » 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

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Exercise from Grahams "Ansi Common Lisp"

Post by nuntius » 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.

...

freeside
Posts: 2
Joined: Sat Mar 18, 2017 4:16 am

Re: Exercise from Grahams "Ansi Common Lisp"

Post by freeside » 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.

sylwester
Posts: 133
Joined: Mon Jul 11, 2011 2:53 pm

Re: Exercise from Grahams "Ansi Common Lisp"

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

Post Reply