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)
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))