4.11 Mutable Pairs and Lists

► |  | The Racket Reference

1 | Language Model
2 | Notation for Documentation
3 | Syntactic Forms
4 | Datatypes
5 | Structures
6 | Classes and Objects
7 | Units
8 | Contracts
9 | Pattern Matching
10 | Control Flow
11 | Concurrency and Parallelism
12 | Macros
13 | Input and Output
14 | Reflection and Security
15 | Operating System
16 | Memory Management
17 | Unsafe Operations
18 | Running Racket
| Bibliography
| Index

▼ | 4 | Datatypes

4.1 | Equality
4.2 | Booleans
4.3 | Numbers
4.4 | Strings
4.5 | Byte Strings
4.6 | Characters
4.7 | Symbols
4.8 | Regular Expressions
4.9 | Keywords
4.10 | Pairs and Lists
4.11 | Mutable Pairs and Lists
4.12 | Vectors
4.13 | Stencil Vectors
4.14 | Boxes
4.15 | Hash Tables
4.16 | Treelists
4.17 | Sequences and Streams
4.18 | Dictionaries
4.19 | Sets
4.20 | Procedures
4.21 | Void
4.22 | Undefined

► | 4.11 | Mutable Pairs and Lists

4.11.1 | Mutable Pair Constructors and Selectors

On this page:

4.11.1 Mutable Pair Constructors and Selectors
mpair?
mcons
mcar
mcdr
set-mcar!
set-mcdr!

Racket
top contents ← prev up next →

### 4.11 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.

#### 4.11.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) → mpair?
> 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/c

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

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

Changes the mutable pair p so that its second element is v.
top contents ← prev up next →