From: Mad Man
Subject: Newbie Question
Date: 
Message-ID: <_iIZk.25267$Jl5.5348@newsfe19.iad>
Hi,
    I'm just starting with common lisp, and have a somewhat silly question. 
How do I remove the last element in a list? I did something like the 
following, but it seems a little cryptic
(setf my-list '(1 2 3 4 5))
(1 2 3 4 5)
(setf my-list (remove (first (last my-list)) my-list :count 1 :from-end))
(1 2 3 4)


Steve D.

From: Kenny
Subject: Re: Newbie Question
Date: 
Message-ID: <493759c0$0$14273$607ed4bc@cv.net>
Mad Man wrote:
> Hi,
>     I'm just starting with common lisp, and have a somewhat silly question. 
> How do I remove the last element in a list? I did something like the 
> following, but it seems a little cryptic
> (setf my-list '(1 2 3 4 5))
> (1 2 3 4 5)
> (setf my-list (remove (first (last my-list)) my-list :count 1 :from-end))
> (1 2 3 4)

check out butlast. takes a count as well.

kt
From: Thomas A. Russ
Subject: Re: Newbie Question
Date: 
Message-ID: <ymitz9jga5u.fsf@blackcat.isi.edu>
"Mad Man" <········@optonline.net> writes:

> Hi,
>     I'm just starting with common lisp, and have a somewhat silly question. 
> How do I remove the last element in a list? 

Kenny gives you the answer.  But be aware that doing that a lot will
result in very inefficient code.  If you find you need to add or remove
things from the end of a (singly-linked) list very frequently, it is a
sign that you should look for a different data structure, like a
dequeue.

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Kaz Kylheku
Subject: Re: Newbie Question
Date: 
Message-ID: <20081219185124.661@gmail.com>
On 2008-12-04, Thomas A. Russ <···@sevak.isi.edu> wrote:
> "Mad Man" <········@optonline.net> writes:
>
>> Hi,
>>     I'm just starting with common lisp, and have a somewhat silly question. 
>> How do I remove the last element in a list? 
>
> Kenny gives you the answer.  But be aware that doing that a lot will
> result in very inefficient code.  If you find you need to add or remove
> things from the end of a (singly-linked) list very frequently, it is a
> sign that you should look for a different data structure, like a
> dequeue.

Early this year, I wrote a deque implementation (or, really, just the
pop operation) which represents the deque as two lists.

It's now in paste.lisp.org.

http://paste.lisp.org/display/71592
From: ·········@yahoo.com
Subject: Re: Newbie Question
Date: 
Message-ID: <d7efcc4f-3426-48b1-8284-f704f2bd84d9@b38g2000prf.googlegroups.com>
On Dec 3, 9:40 pm, "Mad Man" <········@optonline.net> wrote:
> Hi,
>     I'm just starting with common lisp, and have a somewhat silly question.
> How do I remove the last element in a list? I did something like the
> following, but it seems a little cryptic
> (setf my-list '(1 2 3 4 5))
> (1 2 3 4 5)
> (setf my-list (remove (first (last my-list)) my-list :count 1 :from-end))
> (1 2 3 4)
>
> Steve D.

Ruby:
list = [1,2,3,4,5]
    ==>[1, 2, 3, 4, 5]
list.pop
    ==>5
list
    ==>[1, 2, 3, 4]
From: Rainer Joswig
Subject: Re: Newbie Question
Date: 
Message-ID: <joswig-5161EA.22141404122008@news-europe.giganews.com>
In article 
<····································@b38g2000prf.googlegroups.com>,
 ·········@yahoo.com wrote:

> On Dec 3, 9:40�pm, "Mad Man" <········@optonline.net> wrote:
> > Hi,
> > � � I'm just starting with common lisp, and have a somewhat silly question.
> > How do I remove the last element in a list? I did something like the
> > following, but it seems a little cryptic
> > (setf my-list '(1 2 3 4 5))
> > (1 2 3 4 5)
> > (setf my-list (remove (first (last my-list)) my-list :count 1 :from-end))
> > (1 2 3 4)
> >
> > Steve D.
> 
> Ruby:
> list = [1,2,3,4,5]
>     ==>[1, 2, 3, 4, 5]
> list.pop
>     ==>5
> list
>     ==>[1, 2, 3, 4]


Common Lisp:

(butlast list)

-- 
http://lispm.dyndns.org/
From: smallpond
Subject: Re: Newbie Question
Date: 
Message-ID: <1a670b14-ea2b-426d-9a1c-1d8efd29cd64@a12g2000pro.googlegroups.com>
On Dec 3, 10:40 pm, "Mad Man" <········@optonline.net> wrote:
> Hi,
>     I'm just starting with common lisp, and have a somewhat silly question.
> How do I remove the last element in a list? I did something like the
> following, but it seems a little cryptic
> (setf my-list '(1 2 3 4 5))
> (1 2 3 4 5)
> (setf my-list (remove (first (last my-list)) my-list :count 1 :from-end))
> (1 2 3 4)
>
> Steve D.

'(1 2 3 4 5) is a literal object - ie. you are telling lisp it is a
constant.

Even tho lisp won't believe you, it is "more correct" to do (list 1 2
3 4 5)
if you intend to modify it.
From: Thomas A. Russ
Subject: Re: Newbie Question
Date: 
Message-ID: <ymiabbbzgzd.fsf@blackcat.isi.edu>
smallpond <·········@juno.com> writes:

> On Dec 3, 10:40 pm, "Mad Man" <········@optonline.net> wrote:
> > Hi,
> >     I'm just starting with common lisp, and have a somewhat silly question.
> > How do I remove the last element in a list? I did something like the
> > following, but it seems a little cryptic
> > (setf my-list '(1 2 3 4 5))
> > (1 2 3 4 5)
> > (setf my-list (remove (first (last my-list)) my-list :count 1 :from-end))
> > (1 2 3 4)
> >
> > Steve D.
> 
> '(1 2 3 4 5) is a literal object - ie. you are telling lisp it is a
> constant.
> 
> Even tho lisp won't believe you, it is "more correct" to do (list 1 2
> 3 4 5)
> if you intend to modify it.

That would be necessary if using DELETE, but not for REMOVE, since that
creates new list structure rather than modifying the existing structure.

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Kenny
Subject: Re: Newbie Question
Date: 
Message-ID: <493858bb$0$14278$607ed4bc@cv.net>
smallpond wrote:
> On Dec 3, 10:40 pm, "Mad Man" <········@optonline.net> wrote:
> 
>>Hi,
>>    I'm just starting with common lisp, and have a somewhat silly question.
>>How do I remove the last element in a list? I did something like the
>>following, but it seems a little cryptic
>>(setf my-list '(1 2 3 4 5))
>>(1 2 3 4 5)
>>(setf my-list (remove (first (last my-list)) my-list :count 1 :from-end))
>>(1 2 3 4)
>>
>>Steve D.
> 
> 
> '(1 2 3 4 5) is a literal object - ie. you are telling lisp it is a
> constant.
> 
> Even tho lisp won't believe you, it is "more correct" to do (list 1 2
> 3 4 5)
> if you intend to modify it.
> 
> 

I would say (a) one /has to/ use a non-literal unless as in this case 
(b) one is /not/ modifying the list.

Might be wrong on the 'has to', tho.

kt
From: smallpond
Subject: Re: Newbie Question
Date: 
Message-ID: <c4c654ea-7c6b-47be-9edb-b85929ee102e@k8g2000yqn.googlegroups.com>
On Dec 4, 5:24 pm, Kenny <·········@gmail.com> wrote:
> smallpond wrote:
> > On Dec 3, 10:40 pm, "Mad Man" <········@optonline.net> wrote:
>
> >>Hi,
> >>    I'm just starting with common lisp, and have a somewhat silly question.
> >>How do I remove the last element in a list? I did something like the
> >>following, but it seems a little cryptic
> >>(setf my-list '(1 2 3 4 5))
> >>(1 2 3 4 5)
> >>(setf my-list (remove (first (last my-list)) my-list :count 1 :from-end))
> >>(1 2 3 4)
>
> >>Steve D.
>
> > '(1 2 3 4 5) is a literal object - ie. you are telling lisp it is a
> > constant.
>
> > Even tho lisp won't believe you, it is "more correct" to do (list 1 2
> > 3 4 5)
> > if you intend to modify it.
>
> I would say (a) one /has to/ use a non-literal unless as in this case
> (b) one is /not/ modifying the list.
>
> Might be wrong on the 'has to', tho.
>
> kt

(defparameter *a* '(1 2 3 4 5))
*A*
[2]> (delete 3 *a*)
(1 2 4 5)
[3]> *a*
(1 2 4 5)

GNU CLISP 2.43  - maybe other lisp might care.
From: Kenny
Subject: Re: Newbie Question
Date: 
Message-ID: <49399e95$0$14291$607ed4bc@cv.net>
smallpond wrote:
> On Dec 4, 5:24 pm, Kenny <·········@gmail.com> wrote:
> 
>>smallpond wrote:
>>
>>>On Dec 3, 10:40 pm, "Mad Man" <········@optonline.net> wrote:
>>
>>>>Hi,
>>>>   I'm just starting with common lisp, and have a somewhat silly question.
>>>>How do I remove the last element in a list? I did something like the
>>>>following, but it seems a little cryptic
>>>>(setf my-list '(1 2 3 4 5))
>>>>(1 2 3 4 5)
>>>>(setf my-list (remove (first (last my-list)) my-list :count 1 :from-end))
>>>>(1 2 3 4)
>>
>>>>Steve D.
>>
>>>'(1 2 3 4 5) is a literal object - ie. you are telling lisp it is a
>>>constant.
>>
>>>Even tho lisp won't believe you, it is "more correct" to do (list 1 2
>>>3 4 5)
>>>if you intend to modify it.
>>
>>I would say (a) one /has to/ use a non-literal unless as in this case
>>(b) one is /not/ modifying the list.
>>
>>Might be wrong on the 'has to', tho.
>>
>>kt
> 
> 
> (defparameter *a* '(1 2 3 4 5))
> *A*
> [2]> (delete 3 *a*)
> (1 2 4 5)
> [3]> *a*
> (1 2 4 5)
> 
> GNU CLISP 2.43  - maybe other lisp might care.

De facto buys you nothing when it comes to (possibly) undefined 
behavior, and my question is whether or not destructive operations on 
literals is defined to be undefined.

kt
From: smallpond
Subject: Re: Newbie Question
Date: 
Message-ID: <683f2847-8aff-4cfb-ad6e-f2b8ada2663d@f3g2000yqf.googlegroups.com>
On Dec 5, 4:35 pm, Kenny <·········@gmail.com> wrote:
> smallpond wrote:
> > On Dec 4, 5:24 pm, Kenny <·········@gmail.com> wrote:
>
> >>smallpond wrote:
>
> >>>On Dec 3, 10:40 pm, "Mad Man" <········@optonline.net> wrote:
>
> >>>>Hi,
> >>>>   I'm just starting with common lisp, and have a somewhat silly question.
> >>>>How do I remove the last element in a list? I did something like the
> >>>>following, but it seems a little cryptic
> >>>>(setf my-list '(1 2 3 4 5))
> >>>>(1 2 3 4 5)
> >>>>(setf my-list (remove (first (last my-list)) my-list :count 1 :from-end))
> >>>>(1 2 3 4)
>
> >>>>Steve D.
>
> >>>'(1 2 3 4 5) is a literal object - ie. you are telling lisp it is a
> >>>constant.
>
> >>>Even tho lisp won't believe you, it is "more correct" to do (list 1 2
> >>>3 4 5)
> >>>if you intend to modify it.
>
> >>I would say (a) one /has to/ use a non-literal unless as in this case
> >>(b) one is /not/ modifying the list.
>
> >>Might be wrong on the 'has to', tho.
>
> >>kt
>
> > (defparameter *a* '(1 2 3 4 5))
> > *A*
> > [2]> (delete 3 *a*)
> > (1 2 4 5)
> > [3]> *a*
> > (1 2 4 5)
>
> > GNU CLISP 2.43  - maybe other lisp might care.
>
> De facto buys you nothing when it comes to (possibly) undefined
> behavior, and my question is whether or not destructive operations on
> literals is defined to be undefined.
>
> kt

No doubt I just misunderstood "has to".
From: Thomas A. Russ
Subject: Re: Newbie Question
Date: 
Message-ID: <ymi63lyyq05.fsf@blackcat.isi.edu>
smallpond <·········@juno.com> writes:

> (defparameter *a* '(1 2 3 4 5))
> *A*
> [2]> (delete 3 *a*)
> (1 2 4 5)
> [3]> *a*
> (1 2 4 5)
> 
> GNU CLISP 2.43  - maybe other lisp might care.

Well, this doesn't even show that CLISP doesn't care.  It might care
when you compile this in a file and then load it rather than working
solely at the REPL.  In particular, I wouldn't expect a lot of
constant-related effects except in compiled code.


-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Thomas A. Russ
Subject: Re: Newbie Question
Date: 
Message-ID: <ymi1vwmypot.fsf@blackcat.isi.edu>
smallpond <·········@juno.com> writes:
> (defparameter *a* '(1 2 3 4 5))
> *A*
> [2]> (delete 3 *a*)
> (1 2 4 5)
> [3]> *a*
> (1 2 4 5)
> 
> GNU CLISP 2.43  - maybe other lisp might care.

Well, trying the following small program in SBCL, in file /tmp/foo.lisp:

(defparameter *a* '(1 2 3 4 5))
(defparameter *b* '(1 2 3 4 5))

(defun delete-it (location index)
  (delete (nth index location) location))


I get the following results, which indicate that SBCL does take you at
your word when you use constant list structure, and it uses the same
object for both.

* (compile-file "/tmp/foo.lisp")

; compiling file "/tmp/foo.lisp" (written 05 DEC 2008 06:53:43 PM):
; compiling (DEFPARAMETER *A* ...)
; compiling (DEFPARAMETER *B* ...)
; compiling (DEFUN DELETE-IT ...)

; /tmp/foo.fasl written
; compilation finished in 0:00:00
#P"/tmp/foo.fasl"
NIL
NIL

* (load *)
T

* (delete-it *a* 2)
(1 2 4 5)

* *a*
(1 2 4 5)

* *b*
(1 2 4 5)

* (eq *a* *b*)
T


-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Kenny
Subject: Re: Newbie Question
Date: 
Message-ID: <4939f13b$0$20285$607ed4bc@cv.net>
Thomas A. Russ wrote:
> smallpond <·········@juno.com> writes:
> 
>>(defparameter *a* '(1 2 3 4 5))
>>*A*
>>[2]> (delete 3 *a*)
>>(1 2 4 5)
>>[3]> *a*
>>(1 2 4 5)
>>
>>GNU CLISP 2.43  - maybe other lisp might care.
> 
> 
> Well, trying the following small program in SBCL, in file /tmp/foo.lisp:
> 
> (defparameter *a* '(1 2 3 4 5))
> (defparameter *b* '(1 2 3 4 5))
> 
> (defun delete-it (location index)
>   (delete (nth index location) location))
> 
> 
> I get the following results, which indicate that SBCL does take you at
> your word when you use constant list structure, and it uses the same
> object for both.
> 
> * (compile-file "/tmp/foo.lisp")
> 
> ; compiling file "/tmp/foo.lisp" (written 05 DEC 2008 06:53:43 PM):
> ; compiling (DEFPARAMETER *A* ...)
> ; compiling (DEFPARAMETER *B* ...)
> ; compiling (DEFUN DELETE-IT ...)
> 
> ; /tmp/foo.fasl written
> ; compilation finished in 0:00:00
> #P"/tmp/foo.fasl"
> NIL
> NIL
> 
> * (load *)
> T
> 
> * (delete-it *a* 2)
> (1 2 4 5)
> 
> * *a*
> (1 2 4 5)
> 
> * *b*
> (1 2 4 5)
> 
> * (eq *a* *b*)
> T
> 
> 

I'm just worried that it might be undefined behavior, as in demons and 
noses and all that. Not sure how to track that down in the CLHS.

kzo
From: Kaz Kylheku
Subject: Re: Newbie Question
Date: 
Message-ID: <20081221054917.165@gmail.com>
On 2008-12-06, Kenny <·········@gmail.com> wrote:
> I'm just worried that it might be undefined behavior, as in demons and 
> noses and all that. Not sure how to track that down in the CLHS.

My sarcasm and irony detectors are't working, so I will bite: 3.7.1.
From: Kenny
Subject: Re: Newbie Question
Date: 
Message-ID: <493a19a7$0$4870$607ed4bc@cv.net>
Kaz Kylheku wrote:
> On 2008-12-06, Kenny <·········@gmail.com> wrote:
> 
>>I'm just worried that it might be undefined behavior, as in demons and 
>>noses and all that. Not sure how to track that down in the CLHS.
> 
> 
> My sarcasm and irony detectors are't working, so I will bite: 3.7.1.

Ah, demons and noses it is. Thx.

Your sarcasm paranoia is understandable, but I must say the CLHS could 
get a prize for ratio of indexing volume to ease of search.

kt
From: Kaz Kylheku
Subject: Re: Newbie Question
Date: 
Message-ID: <20081221052840.946@gmail.com>
On 2008-12-06, Thomas A. Russ <···@sevak.isi.edu> wrote:
> smallpond <·········@juno.com> writes:
>> (defparameter *a* '(1 2 3 4 5))
>> *A*
>> [2]> (delete 3 *a*)
>> (1 2 4 5)
>> [3]> *a*
>> (1 2 4 5)
>> 
>> GNU CLISP 2.43  - maybe other lisp might care.
>
> Well, trying the following small program in SBCL, in file /tmp/foo.lisp:
>
> (defparameter *a* '(1 2 3 4 5))
> (defparameter *b* '(1 2 3 4 5))
>
> (defun delete-it (location index)
>   (delete (nth index location) location))
>
>
> I get the following results, which indicate that SBCL does take you at
> your word when you use constant list structure, and it uses the same
> object for both.

[ ... ]

> * (eq *a* *b*)
> T

So does this output mean that SBCL cares, or that it doesn't care? Which is it?

Gee, I think that the following C should print 4:

  int i = 3;
  printf("%d\n", i = i++);

I've been /told/ it's not portable, and I can regurgitate this fact in a
newsgroup, but since I'm a butthead, I don't really understand or believe it. 

You see, it's only nonportable to compilers that /care/ that it's undefined,
not ones that don't care. On the don't-care ones it works fine!

How can we tell which kind of compiler I'm working with?

If I see 4 being produced, with no errors or warnings or any other nasty thing,
great. That's the right result, and so this is a compiler that doesn't care
about that silly rule that it's undefined.

If the compiler produces 3, 42, a crash, or a diagnostic message, then it's
one of those non-robust, broken compilers that cares about obscure
language rules.  Such a compiler was contrived just to ``throw the book'' at
great programmers like me, rather than produce the correct result 4.

:)
From: smallpond
Subject: Re: Newbie Question
Date: 
Message-ID: <0d09aa9f-917f-448b-aed9-aa5cf3e91d37@13g2000yql.googlegroups.com>
On Dec 5, 9:58 pm, ····@sevak.isi.edu (Thomas A. Russ) wrote:
> smallpond <·········@juno.com> writes:
> > (defparameter *a* '(1 2 3 4 5))
> > *A*
> > [2]> (delete 3 *a*)
> > (1 2 4 5)
> > [3]> *a*
> > (1 2 4 5)
>
> > GNU CLISP 2.43  - maybe other lisp might care.
>
> Well, trying the following small program in SBCL, in file /tmp/foo.lisp:
>
> (defparameter *a* '(1 2 3 4 5))
> (defparameter *b* '(1 2 3 4 5))
>
> (defun delete-it (location index)
>   (delete (nth index location) location))
>
> I get the following results, which indicate that SBCL does take you at
> your word when you use constant list structure, and it uses the same
> object for both.
>
> * (compile-file "/tmp/foo.lisp")
>
> ; compiling file "/tmp/foo.lisp" (written 05 DEC 2008 06:53:43 PM):
> ; compiling (DEFPARAMETER *A* ...)
> ; compiling (DEFPARAMETER *B* ...)
> ; compiling (DEFUN DELETE-IT ...)
>
> ; /tmp/foo.fasl written
> ; compilation finished in 0:00:00
> #P"/tmp/foo.fasl"
> NIL
> NIL
>
> * (load *)
> T
>
> * (delete-it *a* 2)
> (1 2 4 5)
>
> * *a*
> (1 2 4 5)
>
> * *b*
> (1 2 4 5)
>
> * (eq *a* *b*)
> T
>


Lesson learned.  From now on I will never recommend that
someone use quote instead of list for a modifiable structure.
From: Kaz Kylheku
Subject: Re: Newbie Question
Date: 
Message-ID: <20081221022021.606@gmail.com>
On 2008-12-05, smallpond <·········@juno.com> wrote:
> (defparameter *a* '(1 2 3 4 5))
> *A*
> [2]> (delete 3 *a*)
> (1 2 4 5)
> [3]> *a*
> (1 2 4 5)
>
> GNU CLISP 2.43  - maybe other lisp might care.

Machines don't have feelings.

Please replace ``GNU CLISP 2.43 doesn't care'' with your best approximation to
a technical statement which explains about how exactly GNU CLISP 2.43 responds
to the above usage, in all possible circumstances in which it may occur, and
how that response is different from a situation that would be characterized as
``does care''.

You know, Edsger Dijkstra wrote against the use of anthropomorphizing terms
when we are describing computation. That is to say, talk like ``this function
wants three arguments'' or ``this module believes that this assertion is
true''. He also wrote against the use of debuggers (or ``algorithm animators''
in general) to understand behavior.

You're reasoning about an abstract operation by interactively running a single
instance of it on a single implementation (in its non-compiled mode).  Whatever
inferences you can make from that one example aren't necessarily even correct
for /that/ implementation.
From: Kaz Kylheku
Subject: Re: Newbie Question
Date: 
Message-ID: <20081220000449.641@gmail.com>
On 2008-12-04, smallpond <·········@juno.com> wrote:
> On Dec 3, 10:40 pm, "Mad Man" <········@optonline.net> wrote:
>> Hi,
>>     I'm just starting with common lisp, and have a somewhat silly question.
>> How do I remove the last element in a list? I did something like the
>> following, but it seems a little cryptic
>> (setf my-list '(1 2 3 4 5))
>> (1 2 3 4 5)
>> (setf my-list (remove (first (last my-list)) my-list :count 1 :from-end))
>> (1 2 3 4)
>>
>> Steve D.
>
> '(1 2 3 4 5) is a literal object - ie. you are telling lisp it is a
> constant.
>
> Even tho lisp won't believe you, it is "more correct" to do (list 1 2
> 3 4 5)
> if you intend to modify it.

What's that supposed to mean? Do you intend for your advice to be taken
seriously or not? You write as if /you/ don't believe the definition
of your programming language.

Yes, the Lisp system /will/ treat '(1 2 3 4 5) is a literal.
Respecting this is not some superstitious ritual.

Just because an error is not signaled by a Lisp system doesn't mean
that it doesn't treat literals as real constants.

Can you tell from the language spec what this function should return?

 (defun fun ()
   (let ((list1 '(1 2 3))
         (list2 '(1 2 3)))
     (values (second list2) (setf (second list1) 42) 
             (equal list1 list2) (second list2))))

On the first call? On the second?

Your Lisp system may, but does not have to, do any or all of the following:

- Recognize that two list literals have a common tail, and so
  use the same object for the tail. (In this case,
  the two occurences of '(1 2 3) can simply be the same object).

- Replace the expression (second list1) with the constant 2
  (possibly even where that expression is the of a SETF).

- Signal an error on (setf (second list1) 42), silently discard
  the operation and proceed, or simply crash or behave unpredictably.

- Make different choices for these possibilities in interpreted
  and compiled code!
From: smallpond
Subject: Re: Newbie Question
Date: 
Message-ID: <2603d989-586d-4ca5-bf8a-c55aaf729cff@v38g2000yqb.googlegroups.com>
On Dec 4, 7:39 pm, Kaz Kylheku <········@gmail.com> wrote:
> On 2008-12-04, smallpond <·········@juno.com> wrote:
>
>
>
> > On Dec 3, 10:40 pm, "Mad Man" <········@optonline.net> wrote:
> >> Hi,
> >>     I'm just starting with common lisp, and have a somewhat silly question.
> >> How do I remove the last element in a list? I did something like the
> >> following, but it seems a little cryptic
> >> (setf my-list '(1 2 3 4 5))
> >> (1 2 3 4 5)
> >> (setf my-list (remove (first (last my-list)) my-list :count 1 :from-end))
> >> (1 2 3 4)
>
> >> Steve D.
>
> > '(1 2 3 4 5) is a literal object - ie. you are telling lisp it is a
> > constant.
>
> > Even tho lisp won't believe you, it is "more correct" to do (list 1 2
> > 3 4 5)
> > if you intend to modify it.
>
> What's that supposed to mean? Do you intend for your advice to be taken
> seriously or not? You write as if /you/ don't believe the definition
> of your programming language.
>

Who put the pepper in your Ovaltine?
From: Lars Rune Nøstdal
Subject: Re: Newbie Question
Date: 
Message-ID: <1228416764.29169.161.camel@blackbox.nostdal.org>
On Wed, 2008-12-03 at 21:40 -0600, Mad Man wrote:
> Hi,
>     I'm just starting with common lisp, and have a somewhat silly question. 
> How do I remove the last element in a list? I did something like the 
> following, but it seems a little cryptic
> (setf my-list '(1 2 3 4 5))
> (1 2 3 4 5)
> (setf my-list (remove (first (last my-list)) my-list :count 1 :from-end))
> (1 2 3 4)
> 
> 
> Steve D.


lists are singly linked in lisp, so to avoid lookup i store the
"tail"(?) ..

not tested much (i stopped the second it Worked-for-Me), but anyway:



        (deftype queue () 'cons)
        
        (defconstant +queue-head+ '%queue-head)
          
        
        (defun mk-queue ()
          (let ((head (cons +queue-head+ nil)))
            (cons head head)))
        
        
        (defun queue-add (queue element)
          (declare (queue queue))
          (let ((new-elt (cons element nil)))
            (setf (cddr queue) new-elt 
                  (cdr queue) new-elt)))
        
        
        (defun queue-extract-next (queue)
          (declare (queue queue))
          (unless (eq +queue-head+ (cdr queue))
            (prog1 (pop (cdar queue))
              (setf (cdr queue) (car queue)))))
        
        
        (defun queue-view-next (queue)
          (declare (queue queue))
          (cadar queue))
        
        
        (defun queue-as-list (queue)
          (declare (queue queue))
          (cdar queue))
        
        
        
AMU> (let ((queue (mk-queue)))
       (queue-add queue 1)
       (queue-add queue 2)
       (queue-add queue 3)
       (values (queue-extract-next queue)
               (queue-extract-next queue)
               (queue-extract-next queue)))

1
2
3



..with declerations for speed, no debugging etc. and inlining added it
is fast:


AMU> (let ((queue (mk-queue)))
       (dotimes (i 25000000)
         (queue-add queue i))
       (time (loop :repeat 10000
                :sum (queue-extract-next queue))))

Evaluation took:
  0.000 seconds of real time
  0.000000 seconds of total run time (0.000000 user, 0.000000 system)
  100.00% CPU
  510,994 processor cycles
  0 bytes consed
  
49995000

AMU> (loop :for i :from 0 :below 10000
        :summing i)
49995000