Course four
Raiding the Refrigerator
On the menu: recipes that ask for input — the same program, a different dinner every time.
Take … from refrigerator · the playground's Input panel · ingredients declared without a value.
The refrigerator is the outside world
So far, every number was baked into the recipe. But real programs take input: a game asks for your name, a calculator asks for numbers. In Chef, input lives in the refrigerator:
Take porridge from refrigerator.
This pauses the recipe, reads one number from the input, and stores it in the
ingredient — overwriting whatever was there. Each Take consumes one number.
In the playground, the numbers come from the Input panel below the
editor (separate several with spaces); on a command line they come from whatever you
type or pipe in.
And here's a neat trick with the ingredient list: an ingredient may be declared with no number at all. That's perfect for input — the refrigerator will fill it in. Just don't try to use an empty ingredient before filling it, or the recipe fails on the spot (an empty bag pours nothing).
Tonight's recipe: Porridge Weather Report
Your grandparents abroad still think in Fahrenheit. This recipe takes the morning temperature in Celsius from the refrigerator and serves it back converted: F = C × 9 ÷ 5 + 32. All course-three math — just with an ingredient the recipe doesn't know yet.
Porridge Weather Report.
Takes a temperature in Celsius from the refrigerator and serves it back in Fahrenheit, for grandparents abroad. Multiply before you divide, or the crumbs get lost.
Ingredients.
porridge
9 g ginger
5 g sea salt
32 g maple syrup
Method.
Take porridge from refrigerator. Put porridge into the mixing bowl. Combine ginger into the mixing bowl. Divide sea salt into the mixing bowl. Add maple syrup to the mixing bowl. Pour contents of the mixing bowl into the baking dish.
Serves 1.
20
68
Follow the top of the bowl with input 20:
| Instruction | Top of bowl | Meaning |
|---|---|---|
Take porridge… | — | porridge = 20 (from the fridge) |
Put porridge… | 20 | into the bowl it goes |
Combine ginger… | 180 | × 9 |
Divide sea salt… | 36 | ÷ 5 |
Add maple syrup… | 68 | + 32 — degrees Fahrenheit |
Remember, Chef drops fractions (course three). Try 21 °C: multiply first and you get 21 × 9 = 189, ÷ 5 = 37, + 32 = 69 °F — correct. Divide first and you get 21 ÷ 5 = 4 (ouch), × 9 = 36, + 32 = 68. Same ingredients, sloppier technique, wrong dinner. When division is involved, save it for as late as you can.
Taking letters from the fridge
The refrigerator only hands out numbers — but you already know that characters are numbers in costume. Declare a liquid ingredient with no value, fill it from the fridge, and the number you type comes out dressed as a character:
Mystery Ink.
Type a character code into the Input panel and see who comes out of the fridge.
Ingredients.
ml ink
Method.
Take ink from refrigerator. Put ink into the mixing bowl. Pour contents of the mixing bowl into the baking dish.
Serves 1.
Type 66 into the Input panel and this prints B.
- Feed the weather report 25, then 100 (that's boiling water: 212 °F), then −40. Minus forty is the one temperature where both scales agree — the recipe prints −40 right back.
- Clear the Input panel and run it. The recipe stops with an error the moment
Takefinds the fridge empty. Good to see once, calmly, on purpose. - Write the reverse recipe, Fahrenheit to Celsius: C = (F − 32) × 5 ÷ 9.
You have all the ingredients already —
Removefirst, thenCombine, thenDivide. - Two
Takes, oneAdd: a recipe that reads two numbers (put both in the Input panel, separated by a space) and serves their sum.