Loading...
intermediate
Problem 08 • Step 04
Pair Sugar
pair, head, and tail work, but using them is noisy:
call("head", call(call("pair", 3), 5))
Compare that to what it could be:
head(pair(3, 5))
Write three JavaScript helpers, the same kind of sugar you built before. Each one constructs the right array of calls so you don’t have to nest them by hand.
pair(a, b)builds the curried call to the language-levelpairhead(p)builds a call to the language-levelheadtail(p)builds a call to the language-leveltail
What You’ve Just Built
You built data structures from nothing but functions. No arrays, no objects, no special language features. Just closures. A pair is a function that remembers two values and lets you pick which one you want.
And then you made it readable with one-line sugar functions. head(pair(3, 5)) reads like English, but underneath it’s still arrays feeding into the same evaluator you built from scratch.