top
up

3.8Pairs and ListsπŸ”— i

A pair joins two arbitrary values. The cons procedure constructs pairs, and the car and cdr procedures extract the first and second elements of the pair, respectively. The pair? predicate recognizes pairs.

Some pairs print by wrapping parentheses around the printed forms of the two pair elements, putting a ' at the beginning and a . between the elements.

Examples:
> (cons 12)

'(1 . 2)

> (cons (cons 12)3)

'((1 . 2) . 3)

> (car (cons 12))

1

> (cdr (cons 12))

2

> (pair? (cons 12))

#t

A list is a combination of pairs that creates a linked list. More precisely, a list is either the empty list null , or it is a pair whose first element is a list element and whose second element is a list. The list? predicate recognizes lists. The null? predicate recognizes the empty list.

A list normally prints as a ' followed by a pair of parentheses wrapped around the list elements.

Examples:
> null

'()

> (cons 0(cons 1(cons 2null )))

'(0 1 2)

> (list? null )

#t

> (list? (cons 1(cons 2null )))

#t

> (list? (cons 12))

#f

A list or pair prints using list or cons when one of its elements cannot be written as a quote d value. For example, a value constructed with srcloc cannot be written using quote , and it prints using srcloc:

> (srcloc "file.rkt"101(+ 44))

(srcloc "file.rkt" 1 0 1 8)

> (list 'here(srcloc "file.rkt"1018)'there)

(list 'here (srcloc "file.rkt" 1 0 1 8) 'there)

> (cons 1(srcloc "file.rkt"1018))

(cons 1 (srcloc "file.rkt" 1 0 1 8))

> (cons 1(cons 2(srcloc "file.rkt"1018)))

(list* 1 2 (srcloc "file.rkt" 1 0 1 8))

See also list* .

As shown in the last example, list* is used to abbreviate a series of conses that cannot be abbreviated using list.

The write and display functions print a pair or list without a leading ', cons, list, or list*. There is no difference between write and display for a pair or list, except as they apply to elements of the list:

Examples:
> (write (cons 12))

(1 . 2)

> (display (cons 12))

(1 . 2)

> (write null )

()

()

> (write (list 12"3"))

(1 2 "3")

> (display (list 12"3"))

(1 2 3)

Among the most important predefined procedures on lists are those that iterate through the list’s elements:

> (map (lambda (i)(/ 1i))
'(123))

'(1 1/2 1/3)

> (andmap (lambda (i)(i. < .3))
'(123))

#f

> (ormap (lambda (i)(i. < .3))
'(123))

#t

> (filter (lambda (i)(i. < .3))
'(123))

'(1 2)

> (foldl (lambda (vi)(+ vi))
10
'(123))

16

> (for-each (lambda (i)(display i))
'(123))

123

> (member "Keys"
'("Florida""Keys""U.S.A."))

'("Keys" "U.S.A.")

> (assoc 'where
'((when"3:30")(where"Florida")(who"Mickey")))

'(where "Florida")

+Pairs and Lists in The Racket Reference provides more on pairs and lists.

Pairs are immutable (contrary to Lisp tradition), and pair? and list? recognize immutable pairs and lists, only. The mcons procedure creates a mutable pair, which works with set-mcar! and set-mcdr! , as well as mcar and mcdr . A mutable pair prints using mcons, while write and display print mutable pairs with { and }:

Examples:
> (define p(mcons 12))
> p

(mcons 1 2)

> (pair? p)

#f

> (mpair? p)

#t

> (set-mcar! p0)
> p

(mcons 0 2)

> (write p)

{0 . 2}

+Mutable Pairs and Lists in The Racket Reference provides more on mutable pairs.

top
up

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /