| [ << Scheme tutorial ] | [Top][Contents][Index] | [ Interfaces for programmers >> ] |
| [ < Scheme compound data types ] | [ Up: Scheme compound data types ] | [ Lists > ] |
Pairs
The foundational compound data type of Scheme is the pair. As
might be expected from its name, a pair is two values glued together.
The operator used to form a pair is called cons.
guile> (cons 4 5) (4 . 5) guile>
Note that the pair is displayed as two items surrounded by
parentheses and separated by whitespace, a period (.), and
more whitespace. The period is not a decimal point, but
rather an indicator of the pair.
Pairs can also be entered as literal values by preceding them with a single quote character.
guile> '(4 . 5) (4 . 5) guile>
The two elements of a pair may be any valid Scheme value:
guile> (cons #t #f)
(#t . #f)
guile> '("blah-blah" . 3.1415926535)
("blah-blah" . 3.1415926535)
guile>
The first and second elements of the pair can be accessed by the
Scheme procedures car and cdr, respectively.
guile> (define mypair (cons 123 "hello there") … ) guile> (car mypair) 123 guile> (cdr mypair) "hello there" guile>
Note: cdr is pronounced "could-er", according Sussman and
Abelson, see
https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-14.html#footnote_Temp_133
| [ << Scheme tutorial ] | [Top][Contents][Index] | [ Interfaces for programmers >> ] |
| [ < Scheme compound data types ] | [ Up: Scheme compound data types ] | [ Lists > ] |