Now that we’ve covered the basic concept of programming, particularly that programs are built from the bottom up by combining simple concepts into more complex ones, let me introduce my second lesson. In this lesson I will begin to demonstrate programming concepts through cooking by showing how recipes, sets of instructions for people, can be written more like computer programs, which are sets of instructions for computers. To this end I present “ChefScript,” a loosely formalized language used to represent the logic of a recipe. ChefScript is a pseudocode language, that is, a hybrid of human and computer language used to model a program’s structure prior to implementation. ChefScript will read roughly like a cross between a typical recipe and a programming language.
As my first example of ChefScript, let me write out the recipe for Miso Kale Salad.
Miso Kale Salad Ingredients: Kale Carrots Miso = Red Miso Vinegar = Apple Cider Vinegar Oil = Extra Virgin Olive Oil Onion = One half of a small onion Tofu = Dry-packaged or pressed tofu Parsley = Dried Parsley Tools: Large Salad Bowl Food Processor Main: Remove Ribs (Kale) Slice (Carrots) Large Salad Bowl.Add(Kale,Carrots) Dressing = Make Dressing() Large Salad Bowl.Add(Dressing) Large Salad Bowl.Contents.Massage() Serve(Large Salad Bowl.Contents) end Make Dressing: Slice(Tofu) Slice(Onion) Food Processor.Add(Tofu,Onion,Parsley,Oil,Vinegar) Food Processor.Run() return Food Processor.Contents end
Now let me explain what that all means in English. Hopefully most of it should be pretty clear. Let’s start with Ingredients and Tools.
Ingredients: Kale Carrots Miso = Red Miso Vinegar = Apple Cider Vinegar Oil = Extra Virgin Olive Oil Onion = One half of a small onion Tofu = Dry-packaged or pressed tofu Parsley = Dried Parsley Tools: Large Salad Bowl Food Processor
This says we need Kale, Carrots, Red Miso, Extra Virgin Olive Oil, One half of a small onion, Dry-packaged or pressed tofu, and dried parsley as well as a large salad bowl and a food processor. For clarity, we separate ingredients and tools into two different sections. In a normal recipe, the shorthand for each ingredient would be assumed, but a computer program can’t handle that kind of ambiguity, so complicated ingredients are assigned to variables that represent them later on so that we don’t have to describe them over and over again. For this purpose we use the universal programming symbol =, which means “defined as”. For example, “Onion = one half of a small onion” means, “We will refer to one half of a small onion as ‘Onion’ for the rest of this recipe.”
Next is the main procedure, marked Main. We’ll skip that for now, though. First let’s look at the procedure we’ve defined beneath main. Remember last week when I explained how programs are built upon successive combinations of increasingly complex actions? This is an example. We have defined here a procedure called Make Dressing, which we will refer to in the main procedure. We call this separate definition of an action a function.
Make Dressing: Slice(Tofu) Slice(Onion) Food Processor.Add(Tofu,Onion,Parsley,Oil,Vinegar) Food Processor.Run() return Food Processor.Contents end
In English, this function tells us to slice the tofu, slice the onion, and add all the ingredients other than the kale and the carrots to the food processor. Inside the parentheses of a function are parameters that change what the function does. Slice, for example can take either Tofu or Onion as an argument. It could also take Chicken or Cake. Arguments dramatically increase the versatility of a function. The function “slice” and some others I do not define because I assume you know it or will be able to find out what it means on your own. In a programming language, these would be built-in functions for basic behavior.
Then we run the food processor.
The last line, “return Food Processor.Contents” means that this function will output the contents of the food processor, that is, the dressing, as a result. You’ll see in the main function that the output of this function will be referred to as “dressing”. This is important because in some cases we may want to give the output a different name in the main function, particularly if we happen to make two dressings for two different salads, we could run Make Dressing twice and refer to each output with a different name.
Now for the main procedure.
Main: Remove Ribs (Kale) Slice (Carrots) Large Salad Bowl.Add(Kale,Carrots) Dressing = Make Dressing() Large Salad Bowl.Add(Dressing) Large Salad Bowl.Contents.Massage() Serve(Large Salad Bowl.Contents) end
We begin by removing the kale ribs. Next we slice the carrots and add both kale and carrots to a large salad bowl.
We make the dressing, and add it to the salad bowl with the kale and carrots.
Then we massage the contents of the salad bowl.
Enjoy! In this lesson you’ve learned how to make miso kale salad and also some of the basics of variables and functions in computer programming.