1 Language Model
2 Syntactic Forms
3 Datatypes
4 Structures
5 Classes and Objects
6 Units
7 Contracts
8 Pattern Matching
9 Control Flow
10 Concurrency
11 Macros
12 Input and Output
13 Reflection and Security
14 Operating System
15 Memory Management
16 Running PLT Scheme
Bibliography
Index
On this page:
3.9.1 Pair Constructors and Selectors
pair?
null?
cons
car
cdr
null
list?
list
list*
build-list
3.9.2 List Operations
length
list-ref
list-tail
append
reverse
3.9.3 List Iteration
map
andmap
ormap
for-each
foldl
foldr
3.9.4 List Filtering
filter
remove
remq
remv
remove*
remq*
remv*
sort
3.9.5 List Searching
member
memv
memq
memf
findf
assoc
assv
assq
assf
3.9.6 Pair Accessor Shorthands
caar
cadr
cdar
cddr
caaar
caadr
cadar
caddr
cdaar
cdadr
cddar
cdddr
caaaar
caaadr
caadar
caaddr
cadaar
cadadr
caddar
cadddr
cdaaar
cdaadr
cdadar
cdaddr
cddaar
cddadr
cdddar
cddddr
3.9.7 Additional List Functions and Synonyms
empty
cons?
empty?
first
rest
second
third
fourth
fifth
sixth
seventh
eighth
ninth
tenth
last
last-pair
drop
take
add-between
append*
flatten
remove-duplicates
filter-map
partition
append-map
filter-not
3.9.8 Immutable Cyclic Data
make-reader-graph
placeholder?
make-placeholder
placeholder-set!
placeholder-get
hash-placeholder?
make-hash-placeholder
make-hasheq-placeholder
Version: 4.0.2

 

3.9 Pairs and Lists

    3.9.1 Pair Constructors and Selectors

    3.9.2 List Operations

    3.9.3 List Iteration

    3.9.4 List Filtering

    3.9.5 List Searching

    3.9.6 Pair Accessor Shorthands

    3.9.7 Additional List Functions and Synonyms

    3.9.8 Immutable Cyclic Data

A pair combines exactly two values. The first value is accessed with the car procedure, and the second value is accessed with the cdr procedure. Pairs are not mutable (but see Mutable Pairs and Lists).

A list is recursively defined: it is either the constant null, or it is a pair whose second value is a list.

A list can be used as a single-valued sequence (see Sequences). The elements of the list serve as elements of the sequence. See also in-list.

Cyclic data structures can be created using only immutable pairs via read or make-reader-graph. If starting with a pair and using some number of cdrs returns to the starting pair, then the pair is not a list.

3.9.1 Pair Constructors and Selectors

(pair? v)  boolean?

  v : any/c

Returns #t if v is a pair, #f otherwise.

(null? v)  boolean?

  v : any/c

Returns #t if v is the empty list, #f otherwise.

(cons a d)  pair?

  a : any/c

  d : any/c

Returns a pair whose first element is a and second element is d.

(car p)  any/c

  p : pair?

Returns the first element of the pair p.

(cdr p)  any/c

  p : pair?

Returns the second element of the pair p.

null : null?

The empty list.

(list? v)  boolean?

  v : any/c

Returns #t if v is a list: either the empty list, or a pair whose second element is a list. This procedure takes amortized constant time.

(list v ...)  list?

  v : any/c

Returns a newly allocated list containing the vs as its elements.

(list* v ... tail)  any/c

  v : any/c

  tail : any/c

Like list, but the last argument is used as the tail of the result, instead of the final element. The result is a list only if the last argument is a list.

(build-list n proc)  list?

  n : exact-nonnegative-integer?

  proc : (exact-nonnegative-integer? . -> . any)

Creates a list of n elements by applying proc to the integers from 0 to (sub1 n) in order. If lst is the resulting list, then (list-ref lst i) is the value produced by (proc i).

Examples:

  > (build-list 10 values)

  (0 1 2 3 4 5 6 7 8 9)

  > (build-list 5 (lambda (x) (* x x)))

  (0 1 4 9 16)

3.9.2 List Operations

(length lst)  nonnegative-exact-integer?

  lst : list?

Returns the number of elements in lst.

(list-ref lst pos)  any/c

  lst : any/c

  pos : nonnegative-exact-integer?

Returns the element of lst at position pos, where the list’s first element is position 0. If the list has pos or fewer elements, then the exn:fail:contract exception is raised.

The lst argument need not actually be a list; lst must merely start with a chain of at least pos pairs.

(list-tail lst pos)  any/c

  lst : any/c

  pos : nonnegative-exact-integer?

Returns the list after the first pos elements of lst. If the list has fewer than pos elements, then the exn:fail:contract exception is raised.

The lst argument need not actually be a list; lst must merely start with a chain of at least pos pairs.

(append lst ...)  list?

  lst : list?

(append lst ... v)  any/c

  lst : list?

  v : any/c

When given all list arguments, the result is a lists that contains all of the elements of the given lists in order. The last argument is used directly in the tail of the result.

The last argument need not be a list, in which case the result is an “improper list.”

(reverse lst)  list?

  lst : list?

Returns a list that has the same elements as lst, but in reverse order.

3.9.3 List Iteration

(map proc lst ...+)  list?

  proc : procedure?

  lst : list?

Applies proc to the elements of the lsts from the first elements to the last, returning #f as soon as any application returns #f. The proc argument must accept the same number of arguments as the number of supplied lsts, and all lsts must have the same number of elements. The result is a list containing each result of proc.

(andmap proc lst ...+)  any

  proc : procedure?

  lst : list?

Similar to map, except that

If the lsts are empty, then #t is returned.

Examples:

  > (andmap positive? '(1 2 3))

  #t

  > (andmap positive? '(1 2 a))

  positive?: expects argument of type <real number>; given a

  > (andmap positive? '(1 -2 a))

  #f

  > (andmap + '(1 2 3) '(4 5 6))

  9

(ormap proc lst ...+)  any

  proc : procedure?

  lst : list?

Similar to map, except that

If the lsts are empty, then #f is returned.

Examples:

  > (ormap eq? '(a b c) '(a b c))

  #t

  > (ormap positive? '(1 2 a))

  #t

  > (ormap + '(1 2 3) '(4 5 6))

  5

(for-each proc lst ...+)  void?

  proc : procedure?

  lst : list?

Similar to map, but proc is called only for its effect, and its result (which can be any number of values) is ignored.

(foldl proc init lst ...+)  list?

  proc : procedure?

  init : any/c

  lst : list?

Like map, foldl applies a procedure to the elements of one or more lists. Whereas map combines the return values into a list, foldl combines the return values in an arbitrary way that is determined by proc.

If foldl is called with n lists, then proc must take n+1 arguments. The extra argument is the combined return values so far. The proc is initially invoked with the first item of each list, and the final argument is init. In subsequent invocations of proc, the last argument is the return value from the previous invocation of proc. The input lsts are traversed from left to right, and the result of the whole foldl application is the result of the last application of proc. If the lsts are empty, the result is init.

Unlike foldr, foldl processes the lsts in constant space (plus the space for each call to proc).

Examples:

  > (foldl cons '() '(1 2 3 4))

  (4 3 2 1)

  > (foldl + 0 '(1 2 3 4))

  10

(foldr proc init lst ...+)  list?

  proc : procedure?

  init : any/c

  lst : list?

Like foldl, but the lists are traversed from right to left. Unlike foldl, foldr processes the lsts in space proportional to the length of lsts (plus the space for each call to proc).

Examples:

  > (foldr cons '() '(1 2 3 4))

  (1 2 3 4)

  > (foldr (lambda (v l) (cons (add1 v) l)) '() '(1 2 3 4))

  (2 3 4 5)

3.9.4 List Filtering

(filter pred lst)  list?

  pred : procedure?

  lst : list?

Returns a list with the elements of lst for which pred produces a true value. The pred procedure is applied to each element from first to last.

(remove v lst [proc])  list?

  v : any/c

  lst : list?

  proc : procedure? = equal?

Returns a list that is like lst, omitting the first element of lst that is equal to v using the comparison procedure proc (which must accept two arguments).

(remq v lst)  list?

  v : any/c

  lst : list?

Returns (remove v lst eq?).

(remv v lst)  list?

  v : any/c

  lst : list?

Returns (remove v lst eqv?).

(remove* v-lst lst [proc])  list?

  v-lst : list?

  lst : list?

  proc : procedure? = equal?

Like remove, but removes from lst every instance of every element of v-lst.

(remq* v lst)  list?

  v : any/c

  lst : list?

Returns (remove* v lst eq?).

(remv* v lst)  list?

  v : any/c

  lst : list?

Returns (remove* v lst eqv?).

(sort

 

lst

 

 

 

 

 

 

less-than?

 

 

 

 

 

 [

#:key extract-key

 

 

 

 

 

 

#:cache-keys? cache-keys?])

 

 

list?

  lst : list?

  less-than? : (any/c any/c . -> . any/c)

  extract-key : (any/c . -> . any/c) = (lambda (x) x)

  cache-keys? : boolean? = #f

Returns a list sorted according to the less-than? procedure, which takes two elements of lst and returns a true value if the first is less than (i.e., should be sorted earlier) than the second.

The sort is stable; if two elements of lst are “equal” (i.e., proc does not return a true value when given the pair in either order), then the elements preserve their relative order from lst in the output list. To preserve this guarantee, use sort with a strict comparison functions (e.g., < or string<?; not <= or string<=?).

The #:key argument extract-key is used to extract a key value for comparison from each list element. That is, the full comparison procedure is essentially

  (lambda (x y)

    (less-than? (extract-key x) (extract-key y)))

By default, extract-key is applied to two list elements for every comparison, but if cache-keys? is true, then the extract-key function is used exactly once for each list item. Supply a true value for cache-keys? when extract-key is an expensive operation; for example, if file-or-directory-modify-seconds is used to extract a timestamp for every file in a list, then cache-keys? should be #t to minimize file-system calls, but if extract-key is car, then cache-keys? should be #f. As another example, providing extract-key as (lambda (x) (random)) and #t for cache-keys? effectively shuffles the list.

Examples:

  > (sort '(1 3 4 2) <)

  (1 2 3 4)

  > (sort '("aardvark" "dingo" "cow" "bear") string<?)

  ("aardvark" "bear" "cow" "dingo")

  > (sort '(("aardvark") ("dingo") ("cow") ("bear"))

          #:key car string<?)

  (("aardvark") ("bear") ("cow") ("dingo"))

3.9.5 List Searching

(member v lst)  (or/c list? false/c)

  v : any/c

  lst : list?

Locates the first element of lst that is equal? to v. If such an element exists, the tail of lst starting with that element is returned. Otherwise, the result is #f.

(memv v lst)  (or/c list? false/c)

  v : any/c

  lst : list?

Like member, but finds an element using eqv?.

(memq v lst)  (or/c list? false/c)

  v : any/c

  lst : list?

Like member, but finds an element using eq?.

(memf proc lst)  (or/c list? false/c)

  proc : procedure?

  lst : list?

Like member, but finds an element using the predicate proc; an element is found when proc applied to the element returns a true value.

(findf proc lst)  any/c

  proc : procedure?

  lst : list?

Like memf, but returns the element or #f instead of a tail of lst or #f.

(assoc v lst)  (or/c pair? false/c)

  v : any/c

  lst : (listof pair?)

Locates the first element of lst whose car is equal? to v. If such an element exists, the pair (i.e., an element of lst) is returned. Otherwise, the result is #f.

(assv v lst)  (or/c pair? false/c)

  v : any/c

  lst : (listof pair?)

Like assoc, but finds an element using eqv?.

(assq v lst)  (or/c pair? false/c)

  v : any/c

  lst : (listof pair?)

Like assoc, but finds an element using eq?.

(assf proc lst)  (or/c list? false/c)

  proc : procedure?

  lst : list?

Like assoc, but finds an element using the predicate proc; an element is found when proc applied to the car of an lst element returns a true value.

3.9.6 Pair Accessor Shorthands

(caar v)  any/c

  v : (cons/c pair? any/c)

Returns (car (car p))

(cadr v)  any/c

  v : (cons/c any/c pair?)

Returns (car (cdr p))

(cdar v)  any/c

  v : (cons/c pair? any/c)

Returns (cdr (car p))

(cddr v)  any/c

  v : (cons/c any/c pair?)

Returns (cdr (cdr p))

(caaar v)  any/c

  v : (cons/c (cons/c pair? any/c) any/c)

Returns (car (car (car p)))

(caadr v)  any/c

  v : (cons/c any/c (cons/c pair? any/c))

Returns (car (car (cdr p)))

(cadar v)  any/c

  v : (cons/c (cons/c any/c pair?) any/c)

Returns (car (cdr (car p)))

(caddr v)  any/c

  v : (cons/c any/c (cons/c any/c pair?))

Returns (car (cdr (cdr p)))

(cdaar v)  any/c

  v : (cons/c (cons/c pair? any/c) any/c)

Returns (cdr (car (car p)))

(cdadr v)  any/c

  v : (cons/c any/c (cons/c pair? any/c))

Returns (cdr (car (cdr p)))

(cddar v)  any/c

  v : (cons/c (cons/c any/c pair?) any/c)

Returns (cdr (cdr (car p)))

(cdddr v)  any/c

  v : (cons/c any/c (cons/c any/c pair?))

Returns (cdr (cdr (cdr p)))

(caaaar v)  any/c

  v : (cons/c (cons/c (cons/c pair? any/c) any/c) any/c)

Returns (car (car (car (car p))))

(caaadr v)  any/c

  v : (cons/c any/c (cons/c (cons/c pair? any/c) any/c))

Returns (car (car (car (cdr p))))

(caadar v)  any/c

  v : (cons/c (cons/c any/c (cons/c pair? any/c)) any/c)

Returns (car (car (cdr (car p))))

(caaddr v)  any/c

  v : (cons/c any/c (cons/c any/c (cons/c pair? any/c)))

Returns (car (car (cdr (cdr p))))

(cadaar v)  any/c

  v : (cons/c (cons/c (cons/c any/c pair?) any/c) any/c)

Returns (car (cdr (car (car p))))

(cadadr v)  any/c

  v : (cons/c any/c (cons/c (cons/c any/c pair?) any/c))

Returns (car (cdr (car (cdr p))))

(caddar v)  any/c

  v : (cons/c (cons/c any/c (cons/c any/c pair?)) any/c)

Returns (car (cdr (cdr (car p))))

(cadddr v)  any/c

  v : (cons/c any/c (cons/c any/c (cons/c any/c pair?)))

Returns (car (cdr (cdr (cdr p))))

(cdaaar v)  any/c

  v : (cons/c (cons/c (cons/c pair? any/c) any/c) any/c)

Returns (cdr (car (car (car p))))

(cdaadr v)  any/c

  v : (cons/c any/c (cons/c (cons/c pair? any/c) any/c))

Returns (cdr (car (car (cdr p))))

(cdadar v)  any/c

  v : (cons/c (cons/c any/c (cons/c pair? any/c)) any/c)

Returns (cdr (car (cdr (car p))))

(cdaddr v)  any/c

  v : (cons/c any/c (cons/c any/c (cons/c pair? any/c)))

Returns (cdr (car (cdr (cdr p))))

(cddaar v)  any/c

  v : (cons/c (cons/c (cons/c any/c pair?) any/c) any/c)

Returns (cdr (cdr (car (car p))))

(cddadr v)  any/c

  v : (cons/c any/c (cons/c (cons/c any/c pair?) any/c))

Returns (cdr (cdr (car (cdr p))))

(cdddar v)  any/c

  v : (cons/c (cons/c any/c (cons/c any/c pair?)) any/c)

Returns (cdr (cdr (cdr (car p))))

(cddddr v)  any/c

  v : (cons/c any/c (cons/c any/c (cons/c any/c pair?)))

Returns (cdr (cdr (cdr (cdr p))))

3.9.7 Additional List Functions and Synonyms

 (require scheme/list)

The bindings documented in this section are provided by the scheme/list and scheme libraries, but not scheme/base.

empty : null?

The empty list.

(cons? v)  boolean?

  v : any/c

The same as (pair? v).

(empty? v)  boolean?

  v : any/c

The same as (null? v).

(first lst)  any/c

  lst : list?

The same as (car lst), but only for lists (that are not empty).

(rest lst)  list?

  lst : list?

The same as (cdr lst), but only for lists (that are not empty).

(second lst)  any

  lst : list?

Returns the second element of the list.

(third lst)  any

  lst : list?

Returns the third element of the list.

(fourth lst)  any

  lst : list?

Returns the fourth element of the list.

(fifth lst)  any

  lst : list?

Returns the fifth element of the list.

(sixth lst)  any

  lst : list?

Returns the sixth element of the list.

(seventh lst)  any

  lst : list?

Returns the seventh element of the list.

(eighth lst)  any

  lst : list?

Returns the eighth element of the list.

(ninth lst)  any

  lst : list?

Returns the ninth element of the list.

(tenth lst)  any

  lst : list?

Returns the tenth element of the list.

(last lst)  any

  lst : list?

Returns the last element of the list.

(last-pair p)  pair?

  p : pair?

Returns the last pair of a (possibly improper) list.

(drop lst pos)  list?

  lst : any/c

  pos : nonnegative-exact-integer?

Synonym for list-tail.

(take lst pos)  list?

  lst : any/c

  pos : nonnegative-exact-integer?

Returns a fresh list whose elements are the first pos elements of lst. If lst has fewer than pos elements, the exn:fail:contract exception is raised.

The lst argument need not actually be a list; lst must merely start with a chain of at least pos pairs.

Examples:

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

  (1 2)

  > (take 'non-list 0)

  ()

(add-between lst v)  list?

  lst : list?

  v : any/c

Returns a list with the same elements as lst, but with v between each pair of items in lst.

Examples:

  > (add-between '(x y z) 'or)

  (x or y or z)

  > (add-between '(x) 'or)

  (x)

(append* lst ... lsts)  list?

  lst : list?

  lsts : (listof list?)

(append* lst ... lsts)  any/c

  lst : list?

  lsts : list?

Like append, but the last argument is used as a list of arguments for append, so (append* lst ... lsts) is the same as (apply append lst ... lsts). In other words, the relationship between append and append* is similar to the one between list and list*.

Examples:

  > (append* '(a) '(b) '((c) (d)))

  (a b c d)

  > (cdr (append* (map (lambda (x) (list ", " x))

                       '("Alpha" "Beta" "Gamma"))))

  ("Alpha" ", " "Beta" ", " "Gamma")

(flatten v)  list?

  v : any/c

Flattens an arbitrary S-expression structure of pairs into a list. More precisely, v is treated as a binary tree where pairs are interior nodes, and the resulting list contains all of the non-null leaves of the tree in the same order as an inorder traversal.

Examples:

  > (flatten '((a) b (c (d) . e) ()))

  (a b c d e)

  > (flatten 'a)

  (a)

(remove-duplicates lst [same?])  list?

  lst : list?

  same? : (any/c any/c . -> . any/c) = equal?

Returns a list that has all items in lst, but without duplicate items, where same? determines whether two elements of the list are equivalent. The resulting list is in the same order as lst, and for any item that occurs multiple times, the first one is kept.

Examples:

  > (remove-duplicates '(a b b a))

  (a b)

  > (remove-duplicates '(1 2 1.0 0))

  (1 2 1.0 0)

  > (remove-duplicates '(1 2 1.0 0) =)

  (1 2 0)

(filter-map proc lst ...+)  list?

  proc : procedure?

  lst : list?

Returns (filter (lambda (x) x) (map proc lst ...)), but without building the intermediate list.

(partition pred lst)

 

 

list?

 

list?

  pred : procedure?

  lst : list?

Similar to filter, except that two values are returned: the items for which pred returns a true value, and the items for which pred returns #f.

The result is the same as

  (values (filter pred lst) (filter (negate pred) lst))

but pred is applied to each item in lst only once.

Examples:

  > (partition even? '(1 2 3 4 5 6))

  (2 4 6)

  (1 3 5)

(append-map proc lst ...+)  list?

  proc : procedure?

  lst : list?

Returns (append* (map proc lst ...)).

Examples:

  > (append-map vector->list '(#(1) #(2 3) #(4)))

  (1 2 3 4)

(filter-not pred lst)  list?

  pred : (any/c . -> . any/c)

  lst : list?

Like filter, but the meaning of the pred predicate is reversed: the result is a list of all items for which pred returns #f.

Examples:

  > (filter-not even? '(1 2 3 4 5 6))

  (1 3 5)

3.9.8 Immutable Cyclic Data

(make-reader-graph v)  any/c

  v : any/c

Returns a value like v, with placeholders created by make-placeholder replaced with the values that they contain, and with placeholders created by make-hash-placeholder with an immutable hash table. No part of v is mutated; instead, parts of v are copied as necessary to construct the resulting graph, where at most one copy is created for any given value.

Since the copied vales can be immutable, and since the copy is also immutable, make-reader-graph can cycles involving only immutable pairs, vectors, boxes, and hash tables.

Only the following kinds of values are copied and traversed to detect placeholders:

Due to these restrictions, make-reader-graph creates exactly the same sort of cyclic values as read.

Examples:

  > (let* ([ph (make-placeholder #f)]

           [x (cons 1 ph)])

      (placeholder-set! ph x)

      (make-reader-graph x))

  #0=(1 . #0#)

(placeholder? v)  boolean?

  v : any/c

Returns #t if v is a placeholder created by make-placeholder, #f otherwise.

(make-placeholder v)  placeholder?

  v : any/c

Returns a placeholder for use with placeholder-set! and make-reader-graph. The v argument supplies the initial value for the placeholder.

(placeholder-set! ph datum)  void?

  ph : placeholder?

  datum : any/c

Changes the value of ph to v.

(placeholder-get ph)  any/c

  ph : placeholder?

Returns the value of ph.

(hash-placeholder? v)  boolean?

  v : any/c

Returns #t if v is a placeholder created by make-hash-placeholder, #f otherwise.

(make-hash-placeholder assocs)  hash-placeholder?

  assocs : (listof pair?)

Like make-immutable-hash, but produces a table placeholder for use with make-reader-graph.

(make-hasheq-placeholder assocs)  hash-placeholder?

  assocs : (listof pair?)

Like make-immutable-hasheq, but produces a table placeholder for use with make-reader-graph.