2010/8/6 Yves Parès limestrael@gmail.com:
Still with append!, I have another problem. If my list is initially empty, append! does not alter it:
(define a (list)) (append! a (list 1 2 3))
And a remains empty... I don't know if it is normal
|append!| doesn't mutate the binding for |a|, it mutates the list which is stored at |a| and returns the mutated list.
Unlike a vector, a list is not an object all by itself, instead it is a term used to describe chains of pair objects pairing some other object and the rest of the list, with the null object as the end marker, thus the null object all by itself to represent the empty list case. A pair object can be mutated, which is what (append! (list 'a 'b) (list 1 2 3)) does for the pair containing 'b, but a null object cannot be made to contain anything. So (append! (list) (list 1 2 3)) just returns its second argument. Since you don't mutate the |a| binding, you're not seeing any effect (since there has in fact been none).
What you seem to want to do is:
(set! a (append! a (list 1 2 3)))
Or maybe you want an object containing a (possibly empty) list that can be extended through mutation, in which case you could use a box containing the list:
(define a (box (list))) (define (boxedlist-append! boxedlist list) (set-box! boxedlist (append! (unbox boxedlist) list))) (boxedlist-append! a (list 1 2 3))
But you don't see that done often, because the spirit of Scheme is using functional updates, i.e. passing the extended list to the next function call, so no mutation is necessary.
Christian.