[plt-scheme] append!?

From: jerzy.karczmarczuk at info.unicaen.fr (jerzy.karczmarczuk at info.unicaen.fr)
Date: Sun Oct 21 08:01:20 EDT 2007

Majorinc, Kazimir writes: 

> I'll use append instead of append! for all 
> occasions except if the last list is significantly bigger than other lists 
> that should be appended. In that particular case, use of the append 
> instead of append! would be a sin. Is it sound policy?

Not really.
The append! function - whatever say people who would like to change the
specification of Scheme, is an *economic* procedure, which avoids the
copying of the *left*, first argument. This, especially in a recursive
context, may transform a linear algorithm into quadratic. 

If you think about optimisation, sometimes necessary, then the strategy is
to use append, debug thoroughly the program, analyze it consciously, and
then replace append by append! where you are sure it won't produce havoc 
because of shared pointers...
This means that *usually* I use append! ONLY when it suffices to write
(append! x sthing), without any need for (set! x (append! ...)) 

> Somehow I think that the problem is in simple initial Lisp design mistake, 
> i.e. linked lists that start with cell (cons) instead of pointer to cons 
> (that could be nil for empty list) are good only if one does not need 
> empty list at all. 

Well, a value *stored* somewhere is always a pointer to something, null
or a cons record. And empty lists are used often. I suppose that you would 
like to have everywhere an additional indirection level, as in Smalltalk.
But this is not too economic. If you really wish, use mutable boxes. 

BTW. the lazy Scheme seems to apply this strategy. Everything is inside
promises, which permits to update the contents of an evaluated expression in 
a "no side-effect" way... 

Jerzy Karczmarczuk 




Posted on the users mailing list.