[beginner] About an exercise

Discussion of Common Lisp

[beginner] About an exercise

Postby danielekan » Thu Nov 07, 2013 4:26 am

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))
danielekan
 
Posts: 2
Joined: Wed Nov 06, 2013 12:45 am

Re: [beginner] About an exercise

Postby Goheeca » Fri Nov 08, 2013 3:15 am

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).
User avatar
Goheeca
 
Posts: 198
Joined: Thu May 10, 2012 12:54 pm

Re: [beginner] About an exercise

Postby danielekan » Fri Nov 08, 2013 7:57 am

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

Thank you, I don't know why I didn't see it.
danielekan
 
Posts: 2
Joined: Wed Nov 06, 2013 12:45 am


Return to Common Lisp

Who is online

Users browsing this forum: Google [Bot] and 3 guests

cron