Intro To Clojure Part 1
Intro To Clojure Part 1
Introduction to Clojure
Part 1
Friday, June 7, 13 1
generously sponsored by
JAM
lambdajam.com
Friday, June 7, 13 2
generously sponsored by
juxt.pro
Friday, June 7, 13 3
leiningen
awesome Clojure
project tool
leiningen.org
Friday, June 7, 13 4
Friday, June 7, 13 5
how to use these videos
Friday, June 7, 13 5
how to use these videos
Friday, June 7, 13 5
how to use these videos
Friday, June 7, 13 5
how to use these videos
do the exercises
Friday, June 7, 13 5
how to use these videos
do the exercises
play around
Friday, June 7, 13 5
Friday, June 7, 13 6
REPL
Friday, June 7, 13 6
REPL
Read Eval Print Loop
Friday, June 7, 13 6
REPL
Read Eval Print Loop
Friday, June 7, 13 6
REPL
Read Eval Print Loop
prompt
Friday, June 7, 13 6
REPL
Read Eval Print Loop
prompt form to evaluate
Friday, June 7, 13 6
REPL
Read Eval Print Loop
prompt form to evaluate
Friday, June 7, 13 6
REPL
Read Eval Print Loop
prompt form to evaluate
Friday, June 7, 13 6
REPL
Read Eval Print Loop
prompt form to evaluate
Friday, June 7, 13 6
Friday, June 7, 13 7
commands
Friday, June 7, 13 7
commands
grab
Friday, June 7, 13 7
commands
grab release
Friday, June 7, 13 7
commands
grab release
squeeze
Friday, June 7, 13 7
commands
grab release
squeeze scoop
Friday, June 7, 13 7
commands
grab release
squeeze scoop
add-to-bowl
Friday, June 7, 13 7
commands
grab release
squeeze scoop
add-to-bowl mix
Friday, June 7, 13 7
commands
grab release
squeeze scoop
add-to-bowl mix
pour-into-pan
Friday, June 7, 13 7
commands
grab release
squeeze scoop
add-to-bowl mix
pour-into-pan
bake-pan
Friday, June 7, 13 7
commands
grab release
squeeze scoop
add-to-bowl mix
pour-into-pan
bake-pan cool-pan
Friday, June 7, 13 7
commands
grab release
squeeze scoop
add-to-bowl mix
pour-into-pan
bake-pan cool-pan
status
Friday, June 7, 13 7
commands
grab release
squeeze scoop
add-to-bowl mix
pour-into-pan
bake-pan cool-pan
status start-over
Friday, June 7, 13 7
Friday, June 7, 13 8
function call in Clojure
Friday, June 7, 13 8
function call in Clojure
(grab :cup)
Friday, June 7, 13 8
function call in Clojure
function
(grab :cup)
Friday, June 7, 13 8
function call in Clojure
(grab :cup)
Friday, June 7, 13 8
Friday, June 7, 13 9
keywords
Friday, June 7, 13 9
keywords
:cup is a keyword
Friday, June 7, 13 9
keywords
:cup is a keyword
Friday, June 7, 13 9
keywords
:cup is a keyword
Friday, June 7, 13 9
Friday, June 7, 13 10
inline documentation
Friday, June 7, 13 10
inline documentation
(doc grab)
Friday, June 7, 13 10
inline documentation
function name
(doc grab)
Friday, June 7, 13 10
Friday, June 7, 13 11
cookie recipe
Friday, June 7, 13 11
cookie recipe
1 egg
1 cup flour
1 cup sugar
1 stick butter
mix in bowl
bake in pan 30 minutes
let cool
Friday, June 7, 13 11
Friday, June 7, 13 12
exercise 1
Instruct X5 to bake a cake. Here is the recipe.
2 cups flour
2 eggs
1 cup milk
1 cup sugar
Friday, June 7, 13 13
defn
(defn my-function [a b c d]
(first-step a)
(second-step b c)
(third-step a d))
Friday, June 7, 13 13
defn
function name
(defn my-function [a b c d]
(first-step a)
(second-step b c)
(third-step a d))
Friday, June 7, 13 13
defn
function name parameters
(defn my-function [a b c d]
(first-step a)
(second-step b c)
(third-step a d))
Friday, June 7, 13 13
defn
function name parameters
(defn my-function [a b c d]
(first-step a) body (executed in order)
(second-step b c)
(third-step a d))
Friday, June 7, 13 13
defn
function name parameters
(defn my-function [a b c d]
(first-step a) body (executed in order)
(second-step b c)
(third-step a d)) returns the value
of the last
expression
Friday, June 7, 13 13
Friday, June 7, 13 14
exercise 2
Write a function to add flour, a function to add
milk, and a function to add butter.
Friday, June 7, 13 14
Friday, June 7, 13 15
exercise 3
Friday, June 7, 13 15
Friday, June 7, 13 16
cond
Friday, June 7, 13 16
cond
(cond
(= x 100)
“it’s one hundred”
(> x 10)
“it’s greater than ten”
:else
“it’s something else”)
Friday, June 7, 13 16
cond
(cond
condition (= x 100)
“it’s one hundred”
(> x 10)
“it’s greater than ten”
:else
“it’s something else”)
Friday, June 7, 13 16
cond
(cond
condition (= x 100)
expression “it’s one hundred”
(> x 10)
“it’s greater than ten”
:else
“it’s something else”)
Friday, June 7, 13 16
cond
(cond
condition (= x 100)
expression “it’s one hundred”
condition (> x 10)
“it’s greater than ten”
:else
“it’s something else”)
Friday, June 7, 13 16
cond
(cond
condition (= x 100)
expression “it’s one hundred”
condition (> x 10)
expression “it’s greater than ten”
:else
“it’s something else”)
Friday, June 7, 13 16
cond
(cond
condition (= x 100)
expression “it’s one hundred”
condition (> x 10)
expression “it’s greater than ten”
condition :else
“it’s something else”)
Friday, June 7, 13 16
cond
(cond
condition (= x 100)
expression “it’s one hundred”
condition (> x 10)
expression “it’s greater than ten”
condition :else
expression “it’s something else”)
Friday, June 7, 13 16
Friday, June 7, 13 17
conditional truth
Friday, June 7, 13 17
conditional truth
there are two values that are false
Friday, June 7, 13 17
conditional truth
there are two values that are false
nil and false
Friday, June 7, 13 17
conditional truth
there are two values that are false
nil and false
Friday, June 7, 13 17
conditional truth
there are two values that are false
nil and false
Friday, June 7, 13 17
conditional truth
there are two values that are false
nil and false
Friday, June 7, 13 17
Friday, June 7, 13 18
exercise 4
Friday, June 7, 13 18
Friday, June 7, 13 19
exercise 5
Friday, June 7, 13 19
Friday, June 7, 13 20
exercise 6
Friday, June 7, 13 20
Friday, June 7, 13 21
exercise 7
Write a function simple? which returns true
if the given ingredient (the argument) can be
added without scooping or squeezing
(basically, butter). Otherwise it returns false.
Friday, June 7, 13 21
Friday, June 7, 13 22
if
Friday, June 7, 13 22
if
Friday, June 7, 13 22
if
condition
Friday, June 7, 13 22
if
condition
Friday, June 7, 13 22
if
condition
Friday, June 7, 13 22
Friday, June 7, 13 23
do
Friday, June 7, 13 23
do
(do
(println “first step”)
(println “second step”))
Friday, June 7, 13 23
do
(do
(println “first step”) body
(println “second step”))
Friday, June 7, 13 23
Friday, June 7, 13 24
exercise 8
Write functions add-scooped, add-
squeezed, and add-simple which
conditionally add the respective ingredient
types.
(add-squeezed :egg)
(add-scooped :sugar)
Friday, June 7, 13 24
Friday, June 7, 13 25
dotimes
Friday, June 7, 13 25
dotimes
(dotimes [i 10]
(println i))
Friday, June 7, 13 25
dotimes
name of iteration count
(dotimes [i 10]
(println i))
Friday, June 7, 13 25
dotimes
name of iteration count number of times to iterate
(dotimes [i 10]
(println i))
Friday, June 7, 13 25
dotimes
name of iteration count number of times to iterate
(dotimes [i 10]
(println i)) body
Friday, June 7, 13 25
Friday, June 7, 13 26
exercise 9
add-flour-cups
add-sugar-cups
add-milk-cups
add-butters
Friday, June 7, 13 26
Friday, June 7, 13 27
exercise 10
Friday, June 7, 13 27
Friday, June 7, 13 28
variadic defn
Friday, June 7, 13 28
variadic defn
(defn my-function
([]
“zero-argument version”)
([a]
“one-argument version”)
([a b]
“two-argument version”)
([a b c d]
“four-argument version”))
Friday, June 7, 13 28
variadic defn
function name
(defn my-function
([]
“zero-argument version”)
([a]
“one-argument version”)
([a b]
“two-argument version”)
([a b c d]
“four-argument version”))
Friday, June 7, 13 28
variadic defn
function name
(defn my-function
parameters ([]
“zero-argument version”)
([a]
“one-argument version”)
([a b]
“two-argument version”)
([a b c d]
“four-argument version”))
Friday, June 7, 13 28
variadic defn
function name
(defn my-function
parameters ([]
body “zero-argument version”)
([a]
“one-argument version”)
([a b]
“two-argument version”)
([a b c d]
“four-argument version”))
Friday, June 7, 13 28
variadic defn
function name
(defn my-function
parameters ([]
body “zero-argument version”)
parameters ([a]
“one-argument version”)
([a b]
“two-argument version”)
([a b c d]
“four-argument version”))
Friday, June 7, 13 28
variadic defn
function name
(defn my-function
parameters ([]
body “zero-argument version”)
parameters ([a]
body “one-argument version”)
([a b]
“two-argument version”)
([a b c d]
“four-argument version”))
Friday, June 7, 13 28
variadic defn
function name
(defn my-function
parameters ([]
body “zero-argument version”)
parameters ([a]
body “one-argument version”)
parameters ([a b]
“two-argument version”)
([a b c d]
“four-argument version”))
Friday, June 7, 13 28
variadic defn
function name
(defn my-function
parameters ([]
body “zero-argument version”)
parameters ([a]
body “one-argument version”)
parameters ([a b]
body “two-argument version”)
([a b c d]
“four-argument version”))
Friday, June 7, 13 28
variadic defn
function name
(defn my-function
parameters ([]
body “zero-argument version”)
parameters ([a]
body “one-argument version”)
parameters ([a b]
body “two-argument version”)
parameters ([a b c d]
“four-argument version”))
Friday, June 7, 13 28
variadic defn
function name
(defn my-function
parameters ([]
body “zero-argument version”)
parameters ([a]
body “one-argument version”)
parameters ([a b]
body “two-argument version”)
parameters ([a b c d]
body “four-argument version”))
Friday, June 7, 13 28
Friday, June 7, 13 29
exercise 11
Friday, June 7, 13 29
Friday, June 7, 13 30
exercise 12
Friday, June 7, 13 30
Friday, June 7, 13 31
exercise 13
Write a function bake-cookies
to make it have a structure similar to the
recipe.
1 egg
1 cup flour
1 cup sugar
1 stick butter
mix in bowl
bake in pan 30 minutes
let cool
Friday, June 7, 13 31
Friday, June 7, 13 32
2 eggs
2 cups flour
1 cup milk
1 cup sugar
Friday, June 7, 13 32
(grab :egg) 2 eggs
(squeeze) 2 cups flour
(add-to-bowl) 1 cup milk
(grab :egg)
1 cup sugar
(squeeze)
(add-to-bowl)
(grab :cup) mix all ingredients
(scoop :flour) bake in pan for 25 minutes
(add-to-bowl) let cool
(scoop :flour)
(add-to-bowl)
(scoop :milk)
(add-to-bowl)
(scoop :sugar)
(add-to-bowl)
(release)
(mix)
(pour-into-pan)
(bake-pan 25)
(cool-pan)
Friday, June 7, 13 32
(grab :egg) 2 eggs (add :egg 2)
(squeeze) 2 cups flour (add :flour 2)
(add-to-bowl) 1 cup milk (add :milk 1)
(grab :egg) (add :sugar 1)
1 cup sugar
(squeeze)
(add-to-bowl) (mix)
(grab :cup) mix all ingredients (pour-into-pan)
(scoop :flour) bake in pan for 25 minutes (bake-pan 25)
(add-to-bowl) let cool (cool-pan)
(scoop :flour)
(add-to-bowl)
(scoop :milk)
(add-to-bowl)
(scoop :sugar)
(add-to-bowl)
(release)
(mix)
(pour-into-pan)
(bake-pan 25)
(cool-pan)
Friday, June 7, 13 32
Friday, June 7, 13 33
Friday, June 7, 13 33