Chapter 4Standard MLslides.en.pdf:211

Lists and Tuples

nil, ::, [], (a,b,c)

Def 1.12List constructors
Concept

Lists and tuples are SML's main ways to group values.

Tuples have a fixed length; each component can have a different type.
val p = (1, "one", true);
The type is int * string * bool. Access components with pattern matching:
val (n, s, b) = p; binds n=1, s="one", b=true.

Lists have arbitrary length; all elements must share one type. Per Def 1.12 (slides p.211), lists have two constructors:
- nil (written []) — the empty list.
- :: (pronounced "cons") — prepends one element to a list.

So [1, 2, 3] is sugar for 1 :: 2 :: 3 :: []. The list [1, 2, 3] has type int list; [1, "a"] is a TYPE ERROR because int and string differ.

Pattern matching (Def 1.6) lets you destructure structured values:
val (x, _) = (10, 20); binds x = 10; the _ is an anonymous variable (Def 1.8) — we don't care about that component. This is the foundation for the case expression we'll use on lists and datatypes.

Animation — sml lists
Transcript — click a line to jump8 cues
  1. 0.0sLists in SML
  2. 1.4sPhase one: brackets and a single cell [1]
  3. 3.2sPhase two: cell 2 slides in to make [1, 2]
  4. 5.7sPhase three: cell 3 arrives, list becomes [1, 2, 3]
  5. 8.4sPhase four: cons 4 :: [1, 2, 3] prepends 4
  6. 11.8sPhase five: rev [1, 2, 3] builds [3, 2, 1] off to the right
  7. 15.5sArrows show the reorder; lists are linked cells
  8. 17.0sLists are linked cells; cons prepends; rev reverses
Function spaces

The set of all functions from set AA to set BB is itself a set, written ABA \to B or BAB^A. We can apply set operations to function spaces: BA=BA|B^A| = |B|^{|A|}.

Examples:
- BB\mathbb{B}^{\mathbb{B}} is the set of all boolean functions of one boolean argument. There are 22=42^2 = 4 such functions (constant true, constant false, identity, negation).
- NN\mathbb{N} \to \mathbb{N} is the set of all sequences of naturals.

Indexed families

An indexed family is a set indexed by another set: {ai}iI\{a_i\}_{i \in I} where II is the index set. The order is irrelevant — what matters is the function iaii \mapsto a_i.

Examples:
- The rows of a database table: indexed by row id.
- The coefficients of a polynomial: p(x)=i=0naixip(x) = \sum_{i=0}^n a_i x^i — coefficients {a0,a1,,an}\{a_0, a_1, \ldots, a_n\} indexed by {0,1,,n}\{0, 1, \ldots, n\}.
- The pages of a book: indexed by page numbers.

The indexed family is a function from the index set II to the value set: a:IVa: I \to V with a(i)=aia(i) = a_i.

k-ary relations and functions

A k-ary relation on sets A1,,AkA_1, \ldots, A_k is a subset of A1×A2××AkA_1 \times A_2 \times \cdots \times A_k. So:
- Binary relation: RA×BR \subseteq A \times B (the usual case).
- Ternary relation: RA×B×CR \subseteq A \times B \times C (e.g. 'between': bb is between aa and cc).
- k-ary: RA1××AkR \subseteq A_1 \times \cdots \times A_k.

A k-ary function f:A1××AkBf: A_1 \times \cdots \times A_k \to B takes kk arguments and returns one value. Equivalently, ff corresponds to a (k+1)-ary relation {((a1,,ak),f(a1,,ak))(a1,,ak)A1××Ak}\{((a_1,\ldots,a_k), f(a_1,\ldots,a_k)) \mid (a_1,\ldots,a_k) \in A_1 \times \cdots \times A_k\}.

Practice — score 100% to advance
Multiple choice
Q1
What is the type of (1, "a", true)?
Q2
How many constructors do lists have per Def 1.12?
Q3
What is [1, 2, 3] syntactic sugar for?
Q4
Is [1, "a"] a valid SML list?
Q5
What does _ mean in val (x, _) = (3, 7)?
Q6
What is the type of [] (when its context does not fix the element type)?
Q7
How many functions are in {0,1}{0,1}\{0,1\}^\{0,1\}?
Q8
A ternary relation on A×B×CA \times B \times C is a subset of:
Fill in the blank
Q1
An indexed family is a ___ from the index set to the value set.
Match definitions
Match each concept on the left to its definition on the right.
Loading…