► |  | The Racket Reference

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 and Parallelism
11 | Macros
12 | Input and Output
13 | Reflection and Security
14 | Operating System
15 | Memory Management
16 | Unsafe Operations
17 | Running Racket
| Bibliography
| Index

▼ | 3 | Datatypes

3.1 | Booleans and Equality
3.2 | Numbers
3.3 | Strings
3.4 | Byte Strings
3.5 | Characters
3.6 | Symbols
3.7 | Regular Expressions
3.8 | Keywords
3.9 | Pairs and Lists
3.10 | Mutable Pairs and Lists
3.11 | Vectors
3.12 | Boxes
3.13 | Hash Tables
3.14 | Sequences and Streams
3.15 | Dictionaries
3.16 | Sets
3.17 | Procedures
3.18 | Void and Undefined

► | 3.10 | Mutable Pairs and Lists

3.10.1 | Mutable Pair Constructors and Selectors
3.10.2 | Mutable List Functions

On this page:

3.10.1`` Mutable Pair Constructors and Selectors
mpair?
mcons
mcar
mcdr
set-mcar!
set-mcdr!
3.10.2`` Mutable List Functions
mlist?
mlist
list->mlist
mlist->list
mlength
mlist-ref
mlist-tail
mappend
mappend!
mreverse
mreverse!
mmap
mfor-each
mmember
mmemv
mmemq
massoc
massv
massq
mlistof

top← prev up next →

#### 3.10`` Mutable Pairs and Lists

A mutable pair is like a pair created by cons, but it supports set-mcar! and set-mcdr! mutation operations to change the parts of the mutable pair (like traditional Lisp and Scheme pairs).

A mutable list is analogous to a list created with pairs, but instead created with mutable pairs.

A mutable pair is not a pair; they are completely separate datatypes. Similarly, a mutable list is not a list, except that the empty list is also the empty mutable list. Instead of programming with mutable pairs and mutable lists, data structures such as pairs, lists, and hash tables are practically always better choices.

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

##### 3.10.1`` Mutable Pair Constructors and Selectors

> procedure (mpair? v) → boolean?
> v : any/c

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

> procedure (mcons a d) → pair?
> a : any/c
> d : any/c

Returns a newly allocated mutable pair whose first element is a and second element is d.

> procedure (mcar p) → any/c
> p : mpair?

Returns the first element of the mutable pair p.

> procedure (mcdr p) → any/c
> p : mpair?

Returns the second element of the mutable pair p.

> procedure (set-mcar! p v) → void?
> p : mpair?
> v : any/v

Changes the mutable pair p so that its first element is v.

> procedure (set-mcdr! p v) → void?
> p : mpair?
> v : any/v

Changes the mutable pair p so that its second element is v.

##### 3.10.2`` Mutable List Functions

(require racket/mpair)

The bindings documented in this section are provided by the racket/mpair library, not racket/base or racket.

For functions described in this section, contracts are not directly enforced. In particular, when a mutable list is expected, supplying any other kind of value (or mutating a value that starts as a mutable list) tends to produce an exception from mcar or mcdr.

> procedure (mlist? v) → boolean?
> v : any/c

Returns #t if v is a mutable list: either the empty list, or a mutable pair whose second element is a mutable list.

> procedure (mlist v ...) → mlist?
> v : any/c

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

> procedure (list->mlist lst) → mlist?
> lst : list?

Returns a newly allocated mutable list with the same elements as lst.

> procedure (mlist->list mlst) → list?
> mlst : mlist?

Returns a newly allocated list with the same elements as mlst.

> procedure (mlength mlst) → exact-nonnegative-integer?
> mlst : mlist?

Returns the number of elements in mlst.

> procedure (mlist-ref mlst pos) → any/c
> mlst : mlist?
> pos : exact-nonnegative-integer?

Like list-ref, but for mutable lists.

> procedure (mlist-tail mlst pos) → any/c
> mlst : mlist?
> pos : exact-nonnegative-integer?

Like list-tail, but for mutable lists.

> procedure (mappend mlst ...) → mlist?
> mlst : mlist?
> (mappend mlst ... v) → any/c
> mlst : mlist?
> v : any/c

Like append, but for mutable lists.

> procedure (mappend! mlst ...) → mlist?
> mlst : mlist?
> (mappend! mlst ... v) → any/c
> mlst : mlist?
> v : any/c

The mappend! procedure appends the given mutable lists by mutating the tail of each to refer to the next, using set-mcdr!. Empty lists are dropped; in particular, the result of calling mappend! with one or more empty lists is the same as the result of the call with the empty lists removed from the set of arguments.

> procedure (mreverse mlst) → mlist?
> mlst : mlist?

Like reverse, but for mutable lists.

> procedure (mreverse! mlst) → mlist?
> mlst : mlist?

Like mreverse, but destructively reverses the mutable list by using all of the mutable pairs in mlst and changing them with set-mcdr!.

> procedure (mmap proc mlst ...+) → mlist?
> proc : procedure?
> mlst : mlist?

Like map, but for mutable lists.

> procedure (mfor-each proc mlst ...+) → void?
> proc : procedure?
> mlst : mlist?

Like for-each, but for mutable lists.

> procedure (mmember v mlst) → (or/c mlist? #f)
> v : any/c
> mlst : mlist?

Like member, but for mutable lists.

> procedure (mmemv v mlst) → (or/c mlist? #f)
> v : any/c
> mlst : mlist?

Like memv, but for mutable lists.

> procedure (mmemq v mlst) → (or/c list? #f)
> v : any/c
> mlst : mlist?

Like memq, but for mutable lists.

> procedure (massoc v mlst) → (or/c mpair? #f)
> v : any/c
> mlst : (mlistof mpair?)

Like assoc, but for mutable lists of mutable pairs.

> procedure (massv v mlst) → (or/c mpair? #f)
> v : any/c
> mlst : (mlistof mpair?)

Like assv, but for mutable lists of mutable pairs.

> procedure (massq v mlst) → (or/c mpair? #f)
> v : any/c
> mlst : (mlistof mpair?)

Like assq, but for mutable lists of mutable pairs.

> procedure (mlistof pred) → (any/c . -> . boolean?)
> pred : (any/c . -> . any/c)

Returns a procedure that returns #t when given a mutable list for which pred returns a true value for all elements.
top← prev up next →