Want this course?
Working with the REPL
We start off our journey by baking some cookies and a cake. We learn how to use each ingredient and how to follow recipes.
Read Eval Print Loop
Clojure was designed to support dynamic development. When we program in Clojure, we often program against a live, running system. We "send code" to the Read Eval Print Loop (REPL), the REPL runs it, and we can observe the changes in the system that result. We're going to be running our code in the REPL in this first part.
Let's start a REPL.
At the terminal, make sure you're in the
introduction-to-clojure directory you created with the
git clone command in previous lessons. If you're
not, you can do that with:
$CMD cd introduction-to-clojure $CMD git checkout -f 1.0
$CMD lein repl
This command runs Leiningen with the
repl directive, which starts a
When we run the REPL we see a printout of some command available in the bakery simulation. You can also find these in the reference sheet.
We can run some of these commands. We type the command at the prompt and hit Enter.
(status) will show us the current state of the bakery.
At any point, we can run
(start-over) to reset the state.
When we run the commands in the REPL, we're actually calling functions. Function calls in Clojure are very important. Here's what they look like:
We see that we start with opening parentheses, we name the function
grab), then follow it with the arguments separated by
spaces. Then we close the parentheses. You can have any number of
arguments, including zero. We've already seen a function call with
We see the same pattern:
Open paren, function name, zero arguments, close paren.
You may have noticed the argument I used before when I called
grab. It was
:cup is what is known as a keyword. A
keyword is used in Clojure to name things. It starts with a color
:) and has letters and numbers after that. We'll be using them
soon to name the things we find in the bakery.
Let's bake some cookies to see how this bakery simulator works. Here's the recipe:
Cookies 1 egg 1 cup flour 1 cup sugar 1 stick butter mix in bowl bake in pan 30 minutes let cool
At the REPL, I can follow this recipe.
To use eggs, X5 has to grab the egg, then squeeze it to crack it, then add it to the bowl.
(grab :egg) (squeeze) (add-to-bowl)
To add flour, we will need a cup so we can measure it. We grab the cup, scoop the flour, then add it to the bowl.
(grab :cup) (scoop :flour) (add-to-bowl)
At the end, X5 is still holding the cup.
Sugar also needs to be scooped. X5 already has the cup, so we scoop the sugar, then add it to the bowl. X5 is still holding the cup, so he should release it.
(scoop :sugar) (add-to-bowl) (release)
X5 can simply add a stick of butter. He grabs the butter, then adds it to the bowl.
(grab :butter) (add-to-bowl)
The recipe calls for mixing the ingredients.
Pouring into the pan
X5 will pour the ingredients in the bowl into the pan.
X5 will put the pan in the oven and bake it for a certain number of minutes. The recipe calls for 30 minutes.
Cooling the pan
Then X5 needs to cool the pan.
We should get a message saying we have successfully made some cookies.
If you haven't yet, type in the code for this recipe into the
REPL. You can check the state of the bakery at each step with the
(status) command. And if you get stuck, don't worry. You can run
(start-over) to reset everything.
Instruct X5 to bake a cake. Here is the recipe.
Cake 2 cups flour 2 eggs 1 cup milk 1 cup sugar mix all ingredients bake in pan for 25 minutes let cool
To navigate to this point in the
$CMD git checkout -f 1.0
We can bake the cake at the REPL.
(grab :cup) (scoop :flour) (add-to-bowl) (scoop :flour) (add-to-bowl) (release) (grab :egg) (squeeze) (add-to-bowl) (grab :egg) (squeeze) (add-to-bowl) (grab :cup) (scoop :milk) (add-to-bowl) (scoop :sugar) (add-to-bowl) (release) (mix) (pour-into-pan) (bake-pan 25) (cool-pan)
Code is available: lispcast/introduction-to-clojure
Code for this particular lesson is available at the
1.0 tag .
You can checkout the code in your local repo with this command:
$CMD git clone https://github.com/lispcast/introduction-to-clojure.git $CMD cd introduction-to-clojure $CMD git checkout -f 1.0