Loading...
intermediate
Problem 05 • Step 01
Factorial
This step is different from the previous ones. Instead of writing evaluator rules, you’ll write a program in the language you’ve been building.
The evaluator is already complete. Your job is to define factorial as an expression — a lambda that calls itself.
You need:
["lambda", "n", body]— a function that takesn["if", condition, then, else]— to check the base case["n", "==", 0]— the condition["n", "*", ["factorial", ["n", "-", 1]]]— the recursive case
The function can reference its own name "factorial" in the body because define stores it in the environment before it’s called.
TIP
Think about the two cases: what should happen when n is 0? What should happen otherwise?