Course one
Your First Dish
On the menu: the shape of every Chef recipe, and your very first output.
The five parts of a recipe · ingredients as variables · Put, Pour and Serves · how anything gets printed at all.
The anatomy of a recipe
Every Chef program is laid out exactly like a recipe on a food blog (minus the 2,000-word story about a trip to Tuscany). The parts always appear in the same order, separated by blank lines:
Recipe Title. A free-form comment paragraph. Say anything you like here; the computer skips it entirely. This part is optional. Ingredients. [the ingredient list — one per line] Method. [the actual instructions, written as sentences.] Serves 1.
- The title comes first and ends with a period. Chefs are dramatic like that.
- The comment is optional flavor text. Perfect for jokes.
- Ingredients. declares your variables (more in a second).
- Method. is the program itself. Every instruction is a sentence ending in a period.
- Serves 1. is the magic phrase that actually prints something. Forget it, and your program cooks a lovely meal and then throws it in the bin.
The blank lines between sections are not decoration — they are how Chef tells the sections apart. Squash two sections together and the recipe won't parse.
Tonight's recipe: The Answer Broth
One ingredient, two instructions. This is the “boil water” of Chef programs.
The Answer Broth.
The simplest dish in the book: one ingredient, one bowl, one perfect number. Serve warm to fans of intergalactic hitchhiking.
Ingredients.
42 g mystery beans
Method.
Put mystery beans into the mixing bowl. Pour contents of the mixing bowl into the baking dish.
Serves 1.
42
What just happened?
Start with the ingredient line: 42 g mystery beans. In Chef, an ingredient
is a variable — a named container holding one number. The name is
“mystery beans”, and the number is 42. (The g means grams; measures get
interesting in the next course, so file that away for now.)
Then the method runs, one sentence at a time:
-
Put mystery beans into the mixing bowl.— copies the value 42 into the mixing bowl. The bowl is where all the real cooking happens; ingredients on the shelf never change, but the bowl is fair game. -
Pour contents of the mixing bowl into the baking dish.— copies everything in the bowl to the baking dish. The dish is the serving tray: it's what eventually reaches the table.
Finally, Serves 1. serves one baking dish to the diners — which means:
print everything in it. The dish holds a single 42, so the program prints 42.
Dinner is served.
- Every instruction ends with a period, like a proper sentence.
- Line breaks inside the method don't matter — one long line or one sentence per line, chef's choice. Traditionalists write it as a single breathless paragraph.
- Ingredient names can be several words long (“mystery beans”, “red salmon”) and you must spell them exactly the same way every time.
- A recipe may also list a
Cooking timeand an oven temperature. They do absolutely nothing. They're pure theatre, and yes, they're in the official language specification.
- Open the recipe in the playground and change 42 to your favorite number.
- Add a second
Put mystery beans into the mixing bowl.at the start of the method. The output becomes4242— two servings, printed back to back with nothing in between. Chef doesn't add spaces for you. (Course five has the fix, and it involves a splash of water.) - Rename
mystery beansto something tastier — in both places. Rename it in only one and enjoy your first Chef error message.