[plt-scheme] How to insert an element in empty list?
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)?