From: Xavier Nicollin
Subject: Re: Help before I throw the computer out the window!
Date: 
Message-ID: <Cn65M5.D7y@imag.fr>
In article <··········@bigblue.oit.unc.edu>, ···@tipper.oit.unc.edu (Simon E Spero) writes:
|> In article <················@asl.uni-bielefeld.de>,
|> Andrew P. White <·····@asl.uni-bielefeld.de> wrote:
|> >Hi there, I've recently wandered aimlessly into the world of Lisp (scheme 
|> >actually) directly from C. I'm trying to remove ONLY the last element from 
|> >a of it. I thought about reversing the list, doing (set! list (cdr list)) 
|> 
|> without using destructive modification 
|> 
|> (defun drop-last (list)
|>   "remove the last element from LIST"
|>   (if (or (null list) (null (cdr list)))
|>       nil
|>     (cons (car list) (drop-last (cdr list)))))

With destructive modification, the list must have at least 2 elements.

In Scheme, since it the dialect Andrew uses, you may write:

(define (drop-last! L)
   ;; Removes the last element from L.
   ;; Precondition: (not (or (null? L) (null? (cdr L))))
   (if (null? (cddr L))
      (set-cdr! L ())
      (drop-last! (cdr L))))

+-------------------------------------------------------------------------+
| Xavier NICOLLIN                        | phone  (+33) 76 90 96 41       |
| VERIMAG, Miniparc-ZIRST, rue Lavoisier | fax    (+33) 76 41 36 20       |
| 38330 Montbonnot-Saint-Martin, France  | email  ···············@imag.fr |
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -+
|          "I believe I found the missing link between animal and         |
|           civilized man. It is us."         -- Konrad Lorenz --         |
+-------------------------------------------------------------------------+