Loading...
intermediate
Problem 10 • Step 03
Pretty Print
Same tree. Different meaning.
Write pretty — a function that walks the same expression tree but produces a string instead of a number:
lit(5)becomes"5"makeAdd(lit(2), lit(3))becomes"(2 + 3)"makeAdd(lit(2), makeMul(lit(3), lit(4)))becomes"(2 + (3 * 4))"
Two new tools:
strOf(x)converts a number to a string:strOf(tail("e"))turns42into"42"cc(a, b, c, ...)concatenates multiple values into a string:cc(str("("), str("hello"), str(")"))produces"(hello)"
Notice that pretty has the exact same structure as compute. Check the tag, recurse on children. The only difference is what happens at each node.