From: Eliot Handelman
Subject: Setf Method for LAST
Date: 
Message-ID: <3673@phoenix.Princeton.EDU>
I need a setf method for LAST which should destructively modify the
last cons of a list. For some reason the old beano isn't coming up with
a ready answer. Here's what it should do:

(setq a '(1 2 3))

(setf (last a) '(4)) and now a => (1 2 4)

Using (rplaca (last a) nil) obviously won't do it. Any suggestions?

Thanks!
-- 
This message will cost the net hundreds if not thousands and perhaps millions
billions trillions maybe even zillions of dollars to send everywhere. 
From: Stanley L. Kameny
Subject: Re: Setf Method for LAST
Date: 
Message-ID: <37.UUL1.2#239@valley.UUCP>
Assuming that your lisp uses DE to define a function (if not, you can
readily change this to whatever is appropriate in your version of lisp),
the following should work:

  (DE LASTPP (X)
     (COND ((OR (ATOM X)(ATOM (CDR X))) NIL)
           ((ATOM (CDDR X)) X) (T(LASTPP (CDR X)))))
  (DE RPLALAST(X Y) (PROG2 (RPLACD (LASTPP X) Y) X))

at least, it works on my system:

 (RPLALAST '(1 2 3 4) '(5))
(1 2 3 5)

Stan Kameny  ···········@rand.org   {sm.unisys.com!}randvax!valley!stan
      14314 Hatteras Street, Van Nuys, CA 91401    (818)-994-7767

   Prends moi tel que je suis.        Ricketts House motto (Caltech)
   Honi soit qui mal y pense.         Order of the Garter motto (GB)