Page 1 of 1

[beginner] About an exercise

Posted: Thu Nov 07, 2013 4:26 am
by danielekan
Hello you all.

I am doing the excercises I found in this web page:
http://www.cs.northwestern.edu/academic ... p-exs.html

Working on number fourth, here the text:

Lisp #4: DELETE-CAR (Wilensky, 15)

Define (delete-car list) to modify and return list with the first element of list deleted.

> (setq l (list 'a 'b 'c))
(A B C)
> (delete-car l)
(B C)
> L
(B C)

Note: it's impossible to destructively delete the only item in a list and turn it into NIL, but delete-car should at least return NIL in that case.

I don't understand the note. My code deletes a one item list just fine.

Code: Select all

(defun delete-car (l)
  (if (consp l)
      (let ((temp (cdr l)))
		    (setf (car l) (car temp))
		    (setf (cdr l) (cdr temp))
		l)
      nil))

Re: [beginner] About an exercise

Posted: Fri Nov 08, 2013 3:15 am
by Goheeca
it's impossible to destructively delete the only item in a list and turn it into NIL, but delete-car should at least return NIL in that case.
Your function instead of returning NIL returns NIL as an element of a list.

Re: [beginner] About an exercise

Posted: Fri Nov 08, 2013 7:57 am
by danielekan
I see, it returns '(nil) instead of '().

Thank you, I don't know why I didn't see it.