scheme Syntax Dotted syntax for pairs


Example

There is a particular syntax that allow us to write cons cell in a more compact way than using the cons constructor.

A pair can be written as such:

'(1 . 2) == (cons 1 2)

The big difference is that we can create pairs using quote. Otherwise, Scheme would create a proper list (1 . (2 . '())).

The dot syntax force the expression to have only 2 members. Each member can be of any type including pairs.

'(1 . (2 . (3 . 4)))
> (1 2 3 . 4)

Note that the improper list should be displayed with a dot at the end to show that the cdr of the last pair of the list isn't the empty list '().

This way of showing lists is sometime confusing as the following expression would be expressed not like one would expect it.

'((1 . 2) . ( 3 . 4))
> ((1 . 2) 3 . 4)

Since list usually skip the ., the first argument of the list would be (1 . 2), the second argument would be 3 but since the list is improper, the last . is shown to show that the last element of the list isn't '(). Even thought, the data is shown in a different way, the internal data is as it was created.