Crafting Interpreters · Week 5
Crafting Interpreters 10: Functions: reading guide
Read the chapterVerified by tests
Functions: call expressions, arity checks, native functions like clock(), function declarations, return statements (implemented with an exception), and closures that capture their defining environment.
Time About 7 hours this week
Why it matters
Functions and closures are the heart of most modern languages. Implementing them makes callbacks, higher-order functions and closures in JavaScript or Python fully transparent.
How to approach it
Implement, then write a counter-making function to test closures. Write a recursive Fibonacci and time it with clock() so you have a baseline to compare against clox later.
Challenges
3 challenges at the end of the chapter.
Challenge 2 (anonymous functions) is the best one to implement. Challenge 3 is a short validity puzzle about parameters and locals. Do challenges that change Lox's behavior on a separate git branch. The cracked.guide check runs the official test suite for this chapter, and a challenge that changes semantics (new syntax, different errors) can make tests fail on main.
Traps
- Using an exception for return is correct here; do not try to "fix" it with flags unless you understand the trade-off.
- Binding the closure to the global environment instead of the current one breaks closures.
Verified by the book's chap10_functions tests when you push your repo.