[plt-scheme] How to insert an element in empty list?
jerzy.karczmarczuk at info.unicaen.fr wrote:
> Bill Wood writes:
>> A fairly common technique is to use a so-called "headed list",
>> consisting of a list with a dummy first element. The list is "empty"
>> when the tail of the headed list is empty, you add to the front by
>> inserting a fresh cons containing the new item between the head and the
>> first "real" member (or nil if it is "empty"), etc. This way you need
>> not treat the empty structure as a special case so code is simplified.
>
> This is a trick which may or may not be useful. Still, the empty structure
> IS a different animal, and "simplifying" the code in such a manner -
> 1. Introduces an overhead.
> 2. May make the code less readable, more error prone...
Maybe a box would do?
(http://practical-scheme.net/wiliki/schemexref.cgi?box)
You could then just cons new elements and replace the box contents with
the new list.
--
regards,
Jakub Piotr Cłapa