The Cheffers Cookbook

Course five

Stir Until Done

On the menu: loops — doing a thing over and over until the kitchen timer hits zero.

New kitchen skills

Loops (Verb … until verbed) · countdown timers · Set aside · keeping printed numbers apart with a splash of water.

Every recipe repeats itself

“Knead until smooth.” “Stir until thickened.” Real recipes are full of do-this-until instructions, so Chef's loops look exactly like that. A loop is a matched pair of sentences wrapped around a body:

Launch the rocket.
  [ the loop body — any instructions ]
Launch the rocket until launched.

Here's the mechanism, piece by piece:

(Fine print for future chefs: the ingredient that gets checked is the one in the opening sentence, and the one that gets decremented is the one in the closing sentence. They're usually the same — but they don't have to be, and fancy recipes exploit that.)

The sticky-numbers problem

Before we cook: a warning from course one. When Chef prints several dry numbers, it prints them back to back — a countdown would come out as 54321, which reads more like a zip code than a launch. The fix is pure kitchen logic: put something liquid between them. Water! An ingredient declared as 32 ml water is liquid with value 32 — and 32 is the character code for a space. One splash between every number keeps the plate tidy. (Same trick, different flavor: 10 ml of anything prints as a newline.)

Tonight's recipe: Rocket Salad Countdown

Rocket is a salad leaf (ask a British greengrocer), which makes Launch the rocket. a completely legitimate cooking instruction. This dish prints a five-second countdown and lifts off.

Rocket Salad Countdown · recipe Open in playground ▶
Rocket Salad Countdown.

A zesty salad that counts down from five and launches itself off the plate. A splash of water between the numbers keeps them from sticking together.

Ingredients.
5 g rocket
0 g sugar
1 g salt
32 ml water
76 g lentils
105 g iceberg lettuce
102 g figs
116 g tomatoes
111 g olives
33 g chili flakes

Method.
Put chili flakes into the mixing bowl. Put figs into the mixing bowl. Put figs into the mixing bowl. Put olives into the mixing bowl. Put tomatoes into the mixing bowl. Put figs into the mixing bowl. Put iceberg lettuce into the mixing bowl. Put lentils into the mixing bowl. Liquefy contents of the mixing bowl. Launch the rocket. Put water into the mixing bowl. Put salt into the mixing bowl. Add sugar to the mixing bowl. Fold sugar into the mixing bowl. Put sugar into the mixing bowl. Launch the rocket until launched. Pour contents of the mixing bowl into the baking dish.

Serves 1.
Output
5 4 3 2 1 Liftoff!

How the countdown cooks

Phase one — the message. The first eight Puts spell Liftoff! backwards into the bowl (course two technique: last in, first out), and Liquefy contents melts those eight letters. They'll sit at the bottom of the pile, ready to print last.

Phase two — the loop. rocket starts at 5, so the body runs five times, and the closing sentence lowers the rocket by 1 each lap. Inside the body, a clever little four-step builds the numbers 1, 2, 3, 4, 5:

InstructionEffect
Put water into the mixing bowl.a space, to keep numbers apart
Put salt into the mixing bowl.pushes 1
Add sugar to the mixing bowl.top becomes sugar + 1
Fold sugar into the mixing bowl.scoops the top back into sugar — sugar just grew by one!
Put sugar into the mixing bowl.pushes the new sugar, keeping it on the pile

(That Fold is a sneak preview of course six — it's the opposite of Put: it takes the top value out of the bowl and stores it in an ingredient.) So sugar climbs 1, 2, 3, 4, 5 as the rocket ticks 5, 4, 3, 2, 1 — and because each new number lands on top of the pile, serving prints them biggest-first: 5 4 3 2 1, then the melted letters underneath: Liftoff!

Emergency exits

Two more control moves for your apron pocket. Set aside. bails out of the innermost loop immediately — the “this is burning, moving on” move. And if a loop can never end (say the counter never reaches zero), the Cheffers interpreter won't let your browser stew forever: after 10 million laps it stops the recipe and reports a runtime error. Every chef gets to meet that error once.

Try it
  • Make it a ten-second countdown. One number changes.
  • Delete Put water into the mixing bowl. and watch the launch turn into the zip code 54321Liftoff!
  • Count up instead: replace the four-step sugar trick with a single Put rocket into the mixing bowl. (keep the splash of water). The loop counter itself is an ingredient like any other — the output becomes 1 2 3 4 5 Liftoff! Can you reason out why?
  • Change the closing sentence to lower a different ingredient than the one being checked — then rescue the recipe from the 10-million-lap safety net.