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.

[beginner] About an exercise

3 posts · 3357 views

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.
(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

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.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: [beginner] About an exercise

I see, it returns '(nil) instead of '().

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