1 Welcome to PLT Scheme
2 Scheme Essentials
3 Built-In Datatypes
4 Expressions and Definitions
5 Programmer-Defined Datatypes
6 Modules
7 Input and Output
8 Contracts
9 Classes and Objects
10 Exceptions and Control
11 Iterations and Comprehensions
12 Regular-Expression Matching (Regexps)
13 Pattern Matching
14 Quasiquoting
15 Units (Higher-Order Modules)
16 Threads
17 Syntactic Extension (Macros)
18 Reflection and Dynamic Evaluation
19 Reader Extension
20 Security
21 Memory Management
22 Performance
23 Foreign-Function Interface (FFI)
24 Scripts
25 Graphical User Interfaces (GUIs)
26 More Tools
Index

contents

 index

← prev  up  next →

 

3.8 Pairs and Lists

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 . between them.

Examples:

  > (cons 1 2)

  (1 . 2)

  > (cons (cons 1 2) 3)

  ((1 . 2) . 3)

  > (car (cons 1 2))

  1

  > (cdr (cons 1 2))

  2

  > (pair? (cons 1 2))

  #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 prints as a pair of parentheses wrapped around the list elements.

Examples:

  > null

  ()

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

  (0 1 2)

  > (list? null)

  #t

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

  #t

  > (list? (cons 1 2))

  #f

An expression with ' followed by the printed form of a pair or list produces a pair or list constant.

Examples:

  > '()

  ()

  > '(1 . 2)

  (1 . 2)

  > '(1 2 3)

  (1 2 3)

A pair can be mutable or immutable. Most pairs are immutable (contrary to Lisp tradition), and pair? and list? recognize immutable pairs and lists, only. The mutable-cons procedure creates a mutable pair, which works with set-car! and set-cdr!, as well as car and cdr.

Examples:

  > (define p (mutable-cons 1 2))

  compile: access from an uncertified context to unexported

  variable from module:

  |,/home/scheme/plt/collects/scribblings/guide/pairs| in:

  mutable-cons

  > p

  #<struct:3d-posn>

  > (pair? p)

  #f

  > (set-car! p 0)

  set-car!: expects type <mutable-pair> as 1st argument,

  given: #<struct:3d-posn>; other arguments were: 0

  > p

  #<struct:3d-posn>

Among the most important predefined proecdures on lists are those that iterate through the lists elements:

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

         '(1 2 3))

  (1 1/2 1/3)

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

           '(1 2 3))

  #f

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

           '(1 2 3))

  #t

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

            '(1 2 3))

  (1 2)

  > (foldl (lambda (v i) (+ v i))

           10

           '(1 2 3))

  16

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

              '(1 2 3))

  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 PLT Scheme Reference provides more on pairs and lists.

 

contents

 index

← prev  up  next →