• Skip to main content
  • Skip to footer

PurelyFunctional.tv

  • Learn Clojure
  • About
  • 🛒 Cart
  • Log in
Download
 Video time: 13m43s
PREVIOUS
NEXT

Want this course?

Buy for $48

Working with the REPL

Course: Introduction to Clojure v2

Updated January 15, 2019

Description
Notes
Code

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 REPL.

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.

Function calls

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:

(grab :cup)

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 zero arguments:

(status)

We see the same pattern:

Open paren, function name, zero arguments, close paren.

Keywords

You may have noticed the argument I used before when I called grab. It was :cup. :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.

Baking cookies

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.

Eggs

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)

Flour

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

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)

Butter

X5 can simply add a stick of butter. He grabs the butter, then adds it to the bowl.

(grab :butter)
(add-to-bowl)

Mixing

The recipe calls for mixing the ingredients.

(mix)

Pouring into the pan

X5 will pour the ingredients in the bowl into the pan.

(pour-into-pan)

Baking it

X5 will put the pan in the oven and bake it for a certain number of minutes. The recipe calls for 30 minutes.

(bake-pan 30)

Cooling the pan

Then X5 needs to cool the pan.

(cool-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.

Exercise 1

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 introduction-to-clojure repo:

$CMD git checkout -f 1.0

Watch me

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 branch.

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        

Next Lesson

7 min

Defining functions

We learn how to call functions and define new ones.

Course: Introduction to Clojure v2

0 / 60
0 / 60
 
Lessons
 
Introduction
free  
12 min 
 
Introducing X5 and JC
free  
2 min 
 
Working with the REPL
free  
14 min 
 
Defining functions
 
7 min 
 
Defining a bake-cake function
 
2 min 
 
Conditionals
 
10 min 
 
Defining scooped?
 
2 min 
 
Defining squeezed?
 
3 min 
 
Defining simple?
 
1 min 
 
Defining ingredient types
 
11 min 
 
Looping a number of times
 
10 min 
 
Using the plural functions
 
2 min 
 
Variadic functions
 
15 min 
 
Baking with variadic functions
 
2 min 
 
Bake cookies function
 
2 min 
 
Day 1 Conclusion
 
2 min 
 
Running Leiningen projects
 
11 min 
 
Error function
 
11 min 
 
Refactoring errors
 
4 min 
 
Bakery locations
 
14 min 
 
Scooped?, squeezed?, and simple? refactor
 
2 min 
 
Fetching ingredients
 
9 min 
 
Fetch ingredients generic
 
3 min 
 
Maps
 
7 min 
 
Fetching a shopping list
 
10 min 
 
Refactoring fetch-list
 
10 min 
 
Refactoring locations
 
9 min 
 
A day at the bakery
 
20 min 
 
Analyzing a day at the bakery
 
3 min 
 
Functional programming
 
10 min 
 
Into
 
6 min 
 
multiply-ingredients
 
7 min 
 
order to ingredients
 
5 min 
 
orders to ingredients
 
7 min 
 
Making one shopping trip
 
3 min 
 
Making one delivery
 
12 min 
 
Day 2 conclusion
 
3 min 
 
Day 3
 
2 min 
 
Add cocoa
 
2 min 
 
Baking brownies
 
6 min 
 
Analyzing recipes
 
6 min 
 
Starting the database
 
2 min 
 
Perform
 
1 min 
 
Mix step
 
1 min 
 
Pouring and baking
 
1 min 
 
Adding ingredients
 
7 min 
 
Bake recipe
 
4 min 
 
Generic bake function
 
3 min 
 
Adding all recipes
 
5 min 
 
Cleaning up order to ingredients
 
6 min 
 
Ingredients in the database
 
3 min 
 
Refactoring scooped, squeezed, and simple
 
2 min 
 
Generic fetch ingredient
 
9 min 
 
Refactoring fetch-list (again)
 
10 min 
 
Make storage-location more useful
 
4 min 
 
Redefining add
 
9 min 
 
Defining actions
 
4 min 
 
Refactoring perform
 
4 min 
 
Conclusions
 
5 min 
 
Appendix: Creating a Leiningen project
 
10 min 

Footer CTA

Level up your Clojure skills

The PurelyFunctional.tv Newsletter is a weekly email that helps you improve your Clojure skills through challenges, tips, and news.

Enter your email address to receive emails about Clojure and Functional Programming. These include the weekly newsletter and occasional offers. You can unsubscribe any time.

Copyright © 2021 LispCast and Eric Normand