Re: How to define the append function to add an element into a given list



On Jul 23, 10:54 am, bj...@xxxxxxx (Björn Lindberg) wrote:
The simplest possible queue implementation is a cons cell where car
and cdr points to the first and last cons cell of a list respectively.
Björn Lindberg

You might as well use a structure or class, and face the fact that you
will be doing quite a bit of code to handle going from empty to one
item to two items, and from two to one to empty.

In other words, this is going to be *classic* linked list work that is
not really facilitated by Lisp - gonna be about the same as with C or
assembly.

The easiest, Lispiest way I know is

(defun append-item (queue item) (append queue `(,item)))

But terribly inefficient unless I don't understand something about
implementations.
.



Relevant Pages

  • Re: LISP generalized lists in C
    ... Michael Wojcik wrote: ... In Lisp, the cdr of a list is a list, not an object. ... The cdr of a cons cell in a list is, by definition, a list ...
    (comp.lang.c)
  • Re: newbie exploring better ways
    ... If the list is empty, ... If the car of the list is equal to the item, return the cdr of the ... Else cons the car of the list with the result of calling the function ... How would that look in Lisp? ...
    (comp.lang.lisp)
  • Re: LISP generalized lists in C
    ... In Lisp, the cdr of a list is a list, not an object. ... The cdr of a cons cell in a list is, by definition, a list ... Auden often writes like Disney. ... he does not know what shapes or motivates these beasts. ...
    (comp.lang.c)
  • Re: delete command weirdness
    ... it occurred to me that an implementation could copy the car and cdr of the second cons cell into the car and cdr of the first cons cell -- that would be cute. ... ECLM rant: http://video.google.com/videoplay?docid=-1331906677993764413&hl=en ...
    (comp.lang.lisp)
  • Re: Yet another newB attempting a scheme alg...
    ... R5RS says that it is "an error" to take the car and cdr of an empty ...
    (comp.lang.scheme)