Pairs

The Pair class represents a scheme pair. It consists of a Car and a Cdr field, which can both be any type of object. As far as Scheme itself is concerned, this is all there is to a pair.

However, a scheme pair has a few properties that can be better exposed via .NET interfaces. Consequently, the Pair implements the IList, allowing it to be used in many circumstances where a .NET list class can be. There are some 'oddities' related to the slightly differing nature of a Pair to a 'standard' .NET list which may limit the usefulness of these functions.

Improper lists are not .NET lists. For convienience, no error is generated unless the 'improper' element is accessed, however, so improper lists may be useful in some circumstances. Neither are self-referential lists: these do not produce errors but may result in infinite loops in some cases.

The .NET list interface is designed on the assumption that the 'list' is contained by the class and is not (as in the case of Pairs) the class itself. This means that it is not sensible to remove the only element in a scheme list containing a single value. The .NET 'null' value represents an empty list, and the scheme specification states that an empty list is not a Pair. Use myPair = myPair.Cdr as code to remove the first element in a list wherever you possibly can.

Normally removing (or inserting) an element will just result in an elements Cdr being changed, but this is not always possible, so some operations will result in an element being overwritten. In particular, inserting or removing at the beginning of a Pair list will result in the first element being overwritten with the new value. The consequences are subtle, but may be significant in some circumstances.


Consequences of removing parts of a Pair

Given the pair (1 2 3), stored in the object somePair, the result of somePair.Remove(1) is that somePair.Cdr is changed to point to (3) (the tail of the pair). somePair.Cdr.Remove(0) however, has a new effect. It changes the pair representing (2 3) (the Cdr of pair) with the pair (3). somePair.Cdr is NOT changed, but instead its value is modified.

Pairs can be constructed from any object implementing the ICollection interface. This produces a well-formed scheme list from the contents of the collection.