(guile.info.gz) Pairs
Info Catalog
(guile.info.gz) Compound Data Types
(guile.info.gz) Lists
22.1 Pairs
==========
Pairs are used to combine two Scheme objects into one compound object.
Hence the name: A pair stores a pair of objects.
The data type "pair" is extremely important in Scheme, just like in
any other Lisp dialect. The reason is that pairs are not only used to
make two values available as one object, but that pairs are used for
constructing lists of values. Because lists are so important in Scheme,
they are described in a section of their own ( Lists).
Pairs can literally get entered in source code or at the REPL, in the
so-called "dotted list" syntax. This syntax consists of an opening
parentheses, the first element of the pair, a dot, the second element
and a closing parentheses. The following example shows how a pair
consisting of the two numbers 1 and 2, and a pair containing the symbols
`foo' and `bar' can be entered. It is very important to write the
whitespace before and after the dot, because otherwise the Scheme
parser would not be able to figure out where to split the tokens.
(1 . 2)
(foo . bar)
But beware, if you want to try out these examples, you have to
"quote" the expressions. More information about quotation is available
in the section (REFFIXME). The correct way to try these examples is as
follows.
'(1 . 2)
=>
(1 . 2)
'(foo . bar)
=>
(foo . bar)
A new pair is made by calling the procedure `cons' with two
arguments. Then the argument values are stored into a newly allocated
pair, and the pair is returned. The name `cons' stands for
"construct". Use the procedure `pair?' to test whether a given Scheme
object is a pair or not.
-- Scheme Procedure: cons x y
-- C Function: scm_cons (x, y)
Return a newly allocated pair whose car is X and whose cdr is Y.
The pair is guaranteed to be different (in the sense of `eq?')
from every previously existing object.
-- Scheme Procedure: pair? x
-- C Function: scm_pair_p (x)
Return `#t' if X is a pair; otherwise return `#f'.
The two parts of a pair are traditionally called "car" and "cdr".
They can be retrieved with procedures of the same name (`car' and
`cdr'), and can be modified with the procedures `set-car!' and
`set-cdr!'. Since a very common operation in Scheme programs is to
access the car of a pair, or the car of the cdr of a pair, etc., the
procedures called `caar', `cadr' and so on are also predefined.
-- Scheme Procedure: car pair
-- Scheme Procedure: cdr pair
Return the car or the cdr of PAIR, respectively.
-- Scheme Procedure: caar pair
-- Scheme Procedure: cadr pair ...
-- Scheme Procedure: cdddar pair
-- Scheme Procedure: cddddr pair
These procedures are compositions of `car' and `cdr', where for
example `caddr' could be defined by
(define caddr (lambda (x) (car (cdr (cdr x)))))
-- Scheme Procedure: set-car! pair value
-- C Function: scm_set_car_x (pair, value)
Stores VALUE in the car field of PAIR. The value returned by
`set-car!' is unspecified.
-- Scheme Procedure: set-cdr! pair value
-- C Function: scm_set_cdr_x (pair, value)
Stores VALUE in the cdr field of PAIR. The value returned by
`set-cdr!' is unspecified.
Info Catalog
(guile.info.gz) Compound Data Types
(guile.info.gz) Lists
automatically generated byinfo2html