[plt-scheme] How to insert an element in empty list?

From: Majorinc, Kazimir (kazimir at chem.pmf.hr)
Date: Sun Aug 12 16:58:33 EDT 2007

Chris Laux wrote:
> You could do (cons 'x ()) and would get (x). But I wouldn't call that
> inserting per se. A list is just a chain of cons cells, so maybe you
> have the wrong paradigm?
Maybe.

Say x is my list and it is contained in other lists like y and z.

 > (define x (list 1))
 > (define y (list x))(define z (list x))
 > x
(1)
 > y
((1))
 > z
((1))

Now, when I insert an element in x, the lists y and z (and possibly many 
others) automatically change in the same time.

 > (set-rest! x (list 2))
 > x
(1 2)
 > y
((1 2))
 > z
((1 2))
 >

It is  normal because y and z actually contain that same list. How can I 
make it work the same way if x is initially empty list (not list with 
one element)?


Posted on the users mailing list.