0% found this document useful (0 votes)
10 views

Intro To Clojure Part 3

The document discusses exercises for modifying code to bake recipes using Clojure. It involves writing functions to perform recipe steps, bake recipes, and return the cooling rack ID. It also covers modifying code to look up recipe ingredients from a database instead of using hardcoded values.

Uploaded by

Ajay Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Intro To Clojure Part 3

The document discusses exercises for modifying code to bake recipes using Clojure. It involves writing functions to perform recipe steps, bake recipes, and return the cooling rack ID. It also covers modifying code to look up recipe ingredients from a database instead of using hardcoded values.

Uploaded by

Ajay Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 80

LispCast

Introduction to Clojure
Part 3

Sunday, June 30, 13 1


generously sponsored by

JAM
lambdajam.com

Sunday, June 30, 13 2


generously sponsored by

juxt.pro

Sunday, June 30, 13 3


Sunday, June 30, 13 4
X5’s third day at baking school

Sunday, June 30, 13 4


X5’s third day at baking school

Building a database of baking knowledge

Sunday, June 30, 13 4


X5’s third day at baking school

Building a database of baking knowledge

Designing a language to describe recipes

Sunday, June 30, 13 4


Sunday, June 30, 13 5
cleanup

Remove the add-<ingredient> and


add-<ingredient>s functions. They are not
used.

Sunday, June 30, 13 5


Sunday, June 30, 13 6
warmup

Just to remember how to do it, let’s


add a new ingredient. Cocoa
powder is stored in the pantry and is
scoopable.

Sunday, June 30, 13 6


Sunday, June 30, 13 7
warmup
Add a recipe for brownies.
2 cups flour
2 eggs
1 cup sugar
2 cups cocoa
1 cup milk
2 butters

mix together butter, sugar, and cocoa in a bowl


then add flour, eggs, and milk to same bowl and mix
bake in a pan for 35 minutes
let cool

Sunday, June 30, 13 7


Sunday, June 30, 13 8
2 cups flour
2 eggs
1 cup sugar
2 cups cocoa
1 cup milk
2 butters

mix together butter, sugar, and cocoa in a bowl


then add flour, eggs, and milk to same bowl and mix
bake in a pan for 35 minutes
let cool

Sunday, June 30, 13 8


(defn bake-brownies []
2 cups flour (add :sugar 1)
2 eggs (add :cocoa 2)
(add :butter 2)
1 cup sugar
2 cups cocoa (mix)
1 cup milk
2 butters (add :egg 2)
(add :flour 2)
mix together butter, sugar, and cocoa in a bowl (add :milk 1)
then add flour, eggs, and milk to same bowl and mix
(mix)
bake in a pan for 35 minutes
let cool (pour-into-pan)
(bake-pan 35)
(cool-pan))

Sunday, June 30, 13 8


{:ingredients {:flour 2
2 cups flour :egg 2
2 eggs :sugar 1
1 cup sugar :cocoa 2
2 cups cocoa :milk 1
1 cup milk :butter 2}
2 butters
:steps [[:add :butter]
[:add :sugar]
mix together butter, sugar, and cocoa in a bowl [:add :cocoa]
then add flour, eggs, and milk to same bowl and mix [:mix]
bake in a pan for 35 minutes [:add :flour]
let cool [:add :egg]
[:add :milk]
[:mix]
[:pour]
[:bake 35]
[:cool]]}

Sunday, June 30, 13 8


Sunday, June 30, 13 9
exercise 1

Write a function perform that


takes a vector as argument. If the
first element of the vector
is :cool, run cool-pan.

Sunday, June 30, 13 9


Sunday, June 30, 13 10
exercise 2

Modify perform to also call mix if the first


element of the vector is :mix.

Sunday, June 30, 13 10


Sunday, June 30, 13 11
exercise 3

Modify perform to pour the bowl into the pan


when the first element is :pour. Also, perform
should bake when the first element is :bake. The
number of minutes will be the second element.

Sunday, June 30, 13 11


Sunday, June 30, 13 12
exercise 4
Modify perform to add ingredients to the
bowl if the first element of the vector
is :add. How it operates depends on the
rest of the arguments.

Sunday, June 30, 13 12


exercise 4
Modify perform to add ingredients to the
bowl if the first element of the vector
is :add. How it operates depends on the
rest of the arguments.
If there is one argument and it is the keyword :all, then add all ingredients
in the recipe.
If there is one argument and it is the name of an ingredient in the recipe,
add the amount specified in the ingredients list.
If there are two arguments, the first is the name of the ingredient and the
second is the amount to add.

Sunday, June 30, 13 12


exercise 4
Modify perform to add ingredients to the
bowl if the first element of the vector
is :add. How it operates depends on the
rest of the arguments.
If there is one argument and it is the keyword :all, then add all ingredients
in the recipe.
If there is one argument and it is the name of an ingredient in the recipe,
add the amount specified in the ingredients list.
If there are two arguments, the first is the name of the ingredient and the
second is the amount to add.

Hint: you may need to modify the arguments to perform.

Sunday, June 30, 13 12


Sunday, June 30, 13 13
exercise 5

Write a function bake-recipe which takes


a recipe, performs all of the steps, and
returns the cooling rack id where the item
is placed.

Sunday, June 30, 13 13


Sunday, June 30, 13 14
exercise 6

Rewrite bake to use bake-recipe. It should


still return the cooling rack id.

Sunday, June 30, 13 14


Sunday, June 30, 13 15
exercise 7

Add a recipe for every baked good X5 has


learned so far.

Sunday, June 30, 13 15


Sunday, June 30, 13 16
exercise 8
Modify order->ingredients to not
refer to individual items in the code.
Instead, it should use the meaning
given to the name of the item in the
baking database.

Sunday, June 30, 13 16


Sunday, June 30, 13 17
exercise 9

Add a section to the baking database which


contains information about all of our
ingredients. It should say where to find the
ingredients and how to add them.

Sunday, June 30, 13 17


Sunday, June 30, 13 18
exercise 10

Rewrite scooped?, squeezed?, and


simple? to use the new database instead of
their existing sets.

Sunday, June 30, 13 18


Sunday, June 30, 13 19
exercise 11

There is duplication between fetch-from-pantry


and fetch-from-fridge. Rewrite
fetch-ingredient to not use these functions.
Then get rid of fetch-from-pantry and
fetch-from-fridge.

Sunday, June 30, 13 19


Sunday, June 30, 13 20
group-by

Sunday, June 30, 13 20


group-by

(group-by even? [1 2 3 4 5 6])

Sunday, June 30, 13 20


group-by
function

(group-by even? [1 2 3 4 5 6])

Sunday, June 30, 13 20


group-by
function collection

(group-by even? [1 2 3 4 5 6])

Sunday, June 30, 13 20


group-by
function collection

(group-by even? [1 2 3 4 5 6])

Sunday, June 30, 13 20


group-by
function collection

(group-by even? [1 2 3 4 5 6])


=> {false [1]}

Sunday, June 30, 13 20


group-by
function collection

(group-by even? [1 2 3 4 5 6])


=> {false [1]}

Sunday, June 30, 13 20


group-by
function collection

(group-by even? [1 2 3 4 5 6])


=> {false [1]
true [2]}

Sunday, June 30, 13 20


group-by
function collection

(group-by even? [1 2 3 4 5 6])


=> {false [1]
true [2]}

Sunday, June 30, 13 20


group-by
function collection

(group-by even? [1 2 3 4 5 6])


=> {false [1 3]
true [2]}

Sunday, June 30, 13 20


group-by
function collection

(group-by even? [1 2 3 4 5 6])


=> {false [1 3]
true [2]}

Sunday, June 30, 13 20


group-by
function collection

(group-by even? [1 2 3 4 5 6])


=> {false [1 3]
true [2 4]}

Sunday, June 30, 13 20


group-by
function collection

(group-by even? [1 2 3 4 5 6])


=> {false [1 3]
true [2 4]}

Sunday, June 30, 13 20


group-by
function collection

(group-by even? [1 2 3 4 5 6])


=> {false [1 3 5]
true [2 4]}

Sunday, June 30, 13 20


group-by
function collection

(group-by even? [1 2 3 4 5 6])


=> {false [1 3 5]
true [2 4]}

Sunday, June 30, 13 20


group-by
function collection

(group-by even? [1 2 3 4 5 6])


=> {false [1 3 5]
true [2 4 6]}

Sunday, June 30, 13 20


Sunday, June 30, 13 21
exercise 12

Rewrite fetch-list to remove the usage


of :pantry, :fridge, and the sets
pantry-ingredients and fridge-ingredients.

Hint: Use group-by.

Sunday, June 30, 13 21


exercise 12

Rewrite fetch-list to remove the usage


of :pantry, :fridge, and the sets
pantry-ingredients and fridge-ingredients.

Hint: Use group-by.

Sunday, June 30, 13 21


Sunday, June 30, 13 22
fn

Sunday, June 30, 13 22


fn

(fn [x y] (* x (+ 3 y)))

Sunday, June 30, 13 22


fn
arguments

(fn [x y] (* x (+ 3 y)))

Sunday, June 30, 13 22


fn
arguments body

(fn [x y] (* x (+ 3 y)))

Sunday, June 30, 13 22


Sunday, June 30, 13 23
exercise 13

Rewrite add to use the new


usage map.

Sunday, June 30, 13 23


Sunday, June 30, 13 24
exercise 14
We would like to replace the cond in perform
with a map which follows the same pattern as
the usage map we just wrote. Create a map
actions where the keys are action names and
the values are functions implementing those
actions.

Sunday, June 30, 13 24


Sunday, June 30, 13 25
exercise 15

Rewrite perform to use the new actions


map.

Sunday, June 30, 13 25


Sunday, June 30, 13 26
Sunday, June 30, 13 26
Structure and Interpretation
of Computer Programs

Sunday, June 30, 13 26


Structure and Interpretation Clojure Programming
of Computer Programs

Sunday, June 30, 13 26


Structure and Interpretation Clojure Programming 4clojure.com
of Computer Programs

Sunday, June 30, 13 26


Structure and Interpretation Clojure Programming 4clojure.com clojurekoans.com
of Computer Programs

Sunday, June 30, 13 26


Sunday, June 30, 13 27
Sunday, June 30, 13 27

You might also like