[comp.lang.lisp] How to do set! on lists?

hobbit@huji.ac.il (yoav gonen) (12/12/90)

I've already tried to do set! on lists and it did'n't worked out.
I assume that I must use set-car! or set-cdr! in order to change a list,
but if,for example i want to change a given list to a null one ( == '() )
how can I do that???

barmar@think.com (Barry Margolin) (12/13/90)

In article <hobbit.660999284@shum> hobbit@huji.ac.il (yoav gonen) writes:
>I've already tried to do set! on lists and it did'n't worked out.
>I assume that I must use set-car! or set-cdr! in order to change a list,
>but if,for example i want to change a given list to a null one ( == '() )
>how can I do that???

You can't change a non-null list into a null list.  You must assign '() to
the place that holds the reference to the list.  An empty list is
implemented as the NIL object, while a non-empty list is implemented as a
cons object, and there's no way to change one object into another.

If you have several places containing references to the list, and you want
your change to affect them all, the way to do this is with a level of
indirection.  Instead of assigning the list directly to these places, make
another cons, put the list in its cdr (it doesn't matter what you put in
the car -- you could use this for some kind of self-documentation), and
then assign this cons to all those places.  When you want to use the list,
remember to indirect through the cdr, and if you want to replace the list
you can simply set-cdr! this cons.

I hope this explanation helps, but I'm somewhat doubtful (no offense
intended).  The original question suggests a major lack of understanding of
Lisp data structures, and the above may confuse more than it helps.  I urge
you to seek out a local Lisp/Scheme programmer, or a good Lisp text
(unfortunately, few of the texts use Scheme, so you'll have to deal with
syntactic differences), and get enlightened.


--
Barry Margolin, Thinking Machines Corp.

barmar@think.com
{uunet,harvard}!think!barmar

ballou@databs.enet.dec.com (12/13/90)

In article <1990Dec12.173833.8352@Think.COM>, barmar@think.com (Barry Margolin)
writes:
|> In article <hobbit.660999284@shum> hobbit@huji.ac.il (yoav gonen) writes:
|> >I've already tried to do set! on lists and it did'n't worked out.
|> >I assume that I must use set-car! or set-cdr! in order to change a list,
|> >but if,for example i want to change a given list to a null one ( == '() )
|> >how can I do that???
|> 
|> [...nice explanation of basic lisp data structures...]
|>  
|> I hope this explanation helps, but I'm somewhat doubtful (no offense
|> intended).  The original question suggests a major lack of understanding of
|> Lisp data structures, and the above may confuse more than it helps.  I urge
|> you to seek out a local Lisp/Scheme programmer, or a good Lisp text
|> (unfortunately, few of the texts use Scheme, so you'll have to deal with
|> syntactic differences), and get enlightened.

Well done.  This is the only news group I know in which a novice would
be treated so gently.

My response might have been something like 'buy a clue, buddy'!

Must be working with C/C++ weenies too much...