See it
Visual explainers
The path's core ideas as small pictures you can step through, one sentence per step. Each sits on the reading guides it belongs to; here they are all together.
Tokens
- IF
if - LEFT_PAREN
( - IDENTIFIER
n - GREATER_EQUAL
>=
The next character is '=', so match() consumes it: one GREATER_EQUAL token, not two.
In the guides: Crafting Interpreters 4: ScanningCrafting Interpreters 16: Scanning on Demand
Grammar, lowest precedence first
expression → termterm → factor (( "+" | "-" ) factor)*factor → primary (( "*" | "/" ) primary)*primary → NUMBER
Call stack
- expression()
Built so far
(- (+ 1 (* 2 3)) 4)
No tokens left: expression returns the whole tree, (- (+ 1 (* 2 3)) 4).
In the guides: Crafting Interpreters 5: Representing CodeCrafting Interpreters 6: Parsing ExpressionsCrafting Interpreters 17: Compiling Expressions
- fun makeCounter() {
- var i = 0;
- fun count() {
- i = i + 1;
- print i;
- }
- return count;
- }
- var c = makeCounter();
- c();
- c();
Output nothing yet
Looking up i: it is not in the call's environment, so follow the enclosing pointer and find i = 0 one level up.
In the guides: Crafting Interpreters 8: Statements and StateCrafting Interpreters 10: FunctionsCrafting Interpreters 11: Resolving and BindingCrafting Interpreters 25: Closures
Bytes
Disassembly
| 0000 | OP_CONSTANT | 0 '1' | |
| 0002 | OP_CONSTANT | 1 '2' | |
| 0004 | OP_ADD | ||
| ip | 0005 | OP_NEGATE | |
| 0006 | OP_CONSTANT | 2 '3' | |
| 0008 | OP_MULTIPLY | ||
| 0009 | OP_RETURN |
Constants
- 1
- 2
- 3
Stack
- 3
stackTop
Printed
nothing yet
OP_ADD pops 2 and then 1, adds them and pushes 3.
In the guides: Crafting Interpreters 14: Chunks of BytecodeCrafting Interpreters 15: A Virtual Machine
- 0empty
- 1kiwihome
- 2empty
- 3empty
- 4empty
- 5tombstone
- 6fighome 5
- 7applehome
Slot 6 holds "fig": found, because probing went on past the tombstone at slot 5.
In the guides: Crafting Interpreters 20: Hash TablesTAOCP 6.4 Hashing (part 1)TAOCP 6.4 Hashing (part 2)
Gray stack
- empty
- white: not reached yet
- gray: reached, references not traced
- black: done
Pop C and blacken it: it references nothing new; the gray stack is empty, so marking is done.
In the guides: Crafting Interpreters 26: Garbage CollectionTAOCP 2.3.5 Lists and Garbage Collection
- P ⇐ AVAIL
- INFO(P) ← Y
- LINK(P) ← T
- T ← P
- (pop)
- P ← T
- T ← LINK(P)
- Y ← INFO(P)
- AVAIL ⇐ P
Y none
LINK(P) ← T: the new node points to the old top, B.
In the guides: TAOCP 2.2.1 Stacks, Queues, and DequesTAOCP 2.2.3 Linked Allocation
Inorder so far
DBAEG
Stack (the path from the root)
ACEG
Back at G after its left subtree (empty): inorder visits G now, between its two subtrees.
In the guides: TAOCP 2.3.1 Traversing Binary Trees
Straight insertion 5.2.118 comparisons
Heapsort 5.2.318 comparisons
Two-way merge 5.2.418 comparisons
Straight insertion: 6 > 3, so it moves right; heapsort: 7 rises above 2; two-way merge: 9 ≤ 10, so 9 goes next.
In the guides: TAOCP 5.2.1 Sorting by InsertionTAOCP 5.2.3 Sorting by SelectionTAOCP 5.2.4 Sorting by Merging
Each dot is a pair (Xn, Xn+1)
Xn+1 = (a·Xn + c) mod m
(137·221 + 187) mod 256 = 0
Sequence
1361312146513295146221
Period 256 of a possible 256
Theorem A: a full period needs all three
- c and m share no factor (gcd 1): yes
- a − 1 is divisible by every prime of m (2): yes
- a − 1 is a multiple of 4, since 4 divides m: yes
X256 = 0 was seen before, so the period is 256, the full modulus, and every pair lies on a few parallel lines whose spacing the spectral test measures.
In the guides: TAOCP 3.2.1 The Linear Congruential MethodTAOCP 3.3.4 The Spectral Test
| Option | a | b | c | d | e | f | g |
|---|---|---|---|---|---|---|---|
| c e | |||||||
| a d g | |||||||
| b c f | |||||||
| a d f | |||||||
| b g | |||||||
| d e g |
Chosen, one per level
- {a d f}
- {b g}
- {c e}
Items still to cover
none: solved
Every item is covered: {a d f}, {b g}, {c e} is an exact cover.
In the guides: TAOCP 7.2.2.1 Dancing Links (part 1)TAOCP 7.2.2.1 Dancing Links (part 2)TAOCP 7.2.2.1 Dancing Links (part 3)TAOCP 7.2.2.1 Dancing Links (part 4)
Watch along
Free talks and lectures for each phase, on the speakers' and universities' own channels. Each guide shows the ones for its week.
Crafting Interpreters · weeks 1 to 28
- The Craft Behind Crafting InterpretersBob Nystrom · Handmade Cities · 47 minThe author on why the book builds every line of two interpreters by hand, and how he wrote it. (opens in a new tab)
- Illustrating Crafting InterpretersBob Nystrom · Bob Nystrom on YouTube · 9 minA short look at how the book's hand-drawn diagrams are made, and why pictures carry so much of it. (opens in a new tab)
- Parsing ExplainedDavid Brailsford · Computerphile · 15 minGrammars, ambiguity and parse trees in a quarter of an hour, the week the book writes its parser. (opens in a new tab)
- Lecture 10: Open Addressing, Cryptographic HashingSrini Devadas · MIT OpenCourseWare, 6.006 Fall 2011 · 51 minLinear probing, deletion and load factor on a blackboard, exactly the table clox builds in chapter 20. (opens in a new tab)
- Garbage Collection (Mark & Sweep)Laurence Tratt · Computerphile · 16 minRoots, marking and sweeping explained plainly before you write clox's collector. (opens in a new tab)
TAOCP Vol 1: Fundamental Algorithms · weeks 29 to 63
- Lecture 2: InductionTom Leighton · MIT OpenCourseWare, 6.042J Fall 2010 · 1 h 19 minA patient lecture on induction proofs, the tool section 1.2.1 leans on from its first page. (opens in a new tab)
- Lecture 12: SumsTom Leighton · MIT OpenCourseWare, 6.042J Fall 2010 · 1 h 18 minClosed forms for sums and how to bound the ones without, the moves of section 1.2.3. (opens in a new tab)
- Lecture 15: Linear RecurrencesTom Leighton · MIT OpenCourseWare, 6.042J Fall 2010 · 1 h 18 minSolving recurrences like Fibonacci's, a gentle run-up to sections 1.2.8 and 1.2.9. (opens in a new tab)
- The Analysis of Algorithms (2015, recreating 1969)Donald Knuth · Stanford Online · 54 minKnuth recreating a 1969 lecture on analyzing algorithms, the subject of section 1.2.10. (opens in a new tab)
- The Joy of AsymptoticsDonald Knuth · Stanford Online · 1 h 9 minA Computer Musings lecture on O-notation and asymptotic estimates, the week of section 1.2.11. (opens in a new tab)
- MMIX: A RISC Computer for the New MillenniumDonald Knuth · Stanford Online · 1 h 24 minWhy the books have a machine of their own at all, from MIX to its successor, while you learn MIX. (opens in a new tab)
TAOCP Vol 2: Seminumerical Algorithms · weeks 64 to 89
- Using, Generating and Testing with Pseudo-Random NumbersFrances Buontempo · ACCU Conference 2023 · 56 minHow pseudo-random generators work and how their output gets tested, sections 3.2 and 3.3 in practice. (opens in a new tab)
- Floating Point NumbersTom Scott · Computerphile · 9 minWhy 0.1 + 0.2 is not 0.3, in nine minutes, before Knuth's careful account of rounding. (opens in a new tab)
- Lecture 5: Number Theory IIMarten van Dijk · MIT OpenCourseWare, 6.042J Fall 2010 · 1 h 19 minModular arithmetic and inverses on the board, the ground under section 4.3.2. (opens in a new tab)
- Lecture 11: Integer Arithmetic, Karatsuba MultiplicationSrini Devadas · MIT OpenCourseWare, 6.006 Fall 2011 · 47 minThe first trick that beats schoolbook multiplication, the opening of How Fast Can We Multiply? (opens in a new tab)
- Lecture 4: Number Theory IMarten van Dijk · MIT OpenCourseWare, 6.042J Fall 2010 · 1 h 20 minEuclid's algorithm and linear combinations, the subject of sections 4.5.2 and 4.5.3. (opens in a new tab)
TAOCP Vol 3: Sorting and Searching · weeks 90 to 117
- Lecture 3: Insertion Sort, Merge SortSrini Devadas · MIT OpenCourseWare, 6.006 Fall 2011 · 51 minThe two sorts that open and close section 5.2, with their costs worked out. (opens in a new tab)
- Lecture 4: Heaps and Heap SortSrini Devadas · MIT OpenCourseWare, 6.006 Fall 2011 · 52 minBuilding a heap and sifting down, the heart of sorting by selection. (opens in a new tab)
- Lecture 7: Counting Sort, Radix Sort, Lower Bounds for SortingErik Demaine · MIT OpenCourseWare, 6.006 Fall 2011 · 52 minDistribution sorting and the comparison lower bound, sections 5.2.5 and 5.3.1 side by side. (opens in a new tab)
- Lecture 6: AVL Trees, AVL SortErik Demaine · MIT OpenCourseWare, 6.006 Fall 2011 · 52 minRotations that keep a search tree balanced, drawn step by step for section 6.2.3. (opens in a new tab)
- Lecture 8: Hashing with ChainingErik Demaine · MIT OpenCourseWare, 6.006 Fall 2011 · 51 minHash functions, chaining and simple uniform hashing, a clear start on section 6.4. (opens in a new tab)
- 35 Years of (Linear) ProbingDonald Knuth · Stanford Online · 1 h 11 minKnuth on the analysis of linear probing, which he first worked out in 1962, from section 6.4. (opens in a new tab)
TAOCP Vol 4A: Combinatorial Algorithms, Part 1 · weeks 118 to 133
- Platologic ComputationDonald Knuth · Stanford Online · 1 h 33 minBroadword tricks, many bits at once in one register, from the material that became section 7.1.3. (opens in a new tab)
- Sideways HeapsDonald Knuth · Stanford Online · 1 h 18 minA data structure built from bitwise operations, drawn from section 7.1.3. (opens in a new tab)
- Fun With Binary Decision Diagrams (BDDs)Donald Knuth · Stanford Online · 1 h 42 minKnuth's tour of BDDs and what they can count, the weeks you read section 7.1.4. (opens in a new tab)
- Fun With Zero-Suppressed Binary Decision Diagrams (ZDDs)Donald Knuth · Stanford Online · 1 h 33 minThe ZDD variant for families of sets, the second half of section 7.1.4. (opens in a new tab)
- Integer Partitions and Set Partitions: A Marvelous ConnectionDonald Knuth · Stanford Online · 1 h 30 minHow the objects of sections 7.2.1.4 and 7.2.1.5 turn out to be linked. (opens in a new tab)
- Finding All Spanning TreesDonald Knuth · Stanford Online · 1 h 22 minGenerating every spanning tree of a graph, a centerpiece of section 7.2.1.6. (opens in a new tab)
TAOCP Vol 4B: Combinatorial Algorithms, Part 2 · weeks 134 to 145
- Hamiltonian Paths in AntiquityDonald Knuth · Stanford Online · 1 h 12 minOld puzzles answered by backtracking, a warm companion to section 7.2.2. (opens in a new tab)
- Dancing Links (2018 Christmas Lecture)Donald Knuth · Stanford Online · 1 h 30 minKnuth demonstrates exact cover with dancing links, the four weeks of section 7.2.2.1. (opens in a new tab)
- Dancing Cells (2023 Christmas Lecture)Donald Knuth · Stanford Online · 1 h 33 minThe sequel: a newer data structure for the same exact cover problems. (opens in a new tab)
- The Art of Computer Programming: Satisfiability and CombinatoricsDonald Knuth · Brown University · 2 h 9 minKnuth on SAT solvers and the problems they crack, the subject of section 7.2.2.2. (opens in a new tab)