Loading...
intermediate
Problem 10 • Step 01
Expressions as Pairs
Throughout this module, expressions like [2, "+", 3] had one meaning: compute a value. The expression and its interpretation were tangled together. Now separate them.
An expression is just data — a pair where the first element is a tag saying what kind of node it is, and the second element holds the contents.
Define three functions in the language:
littakes a value and pairs it with the tag"lit"make-addtakes two expressions and pairs the tag"add"with a pair of the two childrenmake-multakes two expressions and pairs the tag"mul"with a pair of the two children
Since make-add and make-mul each need two arguments, they must be curried: fn("a", fn("b", ...)).
Use str("tag") to create string values. This is new — the evaluator now has a quote rule. str("add") produces the string "add" as a value, instead of looking up a variable named add.