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

Intro To Clojure Part 1

The document provides an introduction to Clojure and covers topics like the REPL, keywords, function calls, inline documentation, defining functions, and more. It includes code examples and exercises.

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)
14 views

Intro To Clojure Part 1

The document provides an introduction to Clojure and covers topics like the REPL, keywords, function calls, inline documentation, defining functions, and more. It includes code examples and exercises.

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/ 132

LispCast

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

type in the code yourself

Friday, June 7, 13 5
how to use these videos

type in the code yourself

pause and rewind

Friday, June 7, 13 5
how to use these videos

type in the code yourself

pause and rewind

do the exercises

Friday, June 7, 13 5
how to use these videos

type in the code yourself

pause and rewind

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

user=> (println "Hello, world!")


Hello, world!
nil
user=>

Friday, June 7, 13 6
REPL
Read Eval Print Loop
prompt

user=> (println "Hello, world!")


Hello, world!
nil
user=>

Friday, June 7, 13 6
REPL
Read Eval Print Loop
prompt form to evaluate

user=> (println "Hello, world!")


Hello, world!
nil
user=>

Friday, June 7, 13 6
REPL
Read Eval Print Loop
prompt form to evaluate

user=> (println "Hello, world!")


Hello, world! printed output
nil
user=>

Friday, June 7, 13 6
REPL
Read Eval Print Loop
prompt form to evaluate

user=> (println "Hello, world!")


Hello, world! printed output
nil return value
user=>

Friday, June 7, 13 6
REPL
Read Eval Print Loop
prompt form to evaluate

user=> (println "Hello, world!")


Hello, world! printed output
nil return value
user=> prompt

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

function arguments (0 or more)

(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

keywords start with :

Friday, June 7, 13 9
keywords
:cup is a keyword

keywords start with :

keywords are useful for naming things

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

mix all ingredients


bake in pan for 25 minutes
let cool
Friday, June 7, 13 12
Friday, June 7, 13 13
defn

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.

The functions should be named


add-flour
add-milk
add-butter

Friday, June 7, 13 14
Friday, June 7, 13 15
exercise 3

Write a function bake-cake which uses the


new add- functions.

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

all other values are true

Friday, June 7, 13 17
conditional truth
there are two values that are false
nil and false

all other values are true

nil means “no answer” or “nothing”

Friday, June 7, 13 17
conditional truth
there are two values that are false
nil and false

all other values are true

nil means “no answer” or “nothing”

it is equivalent to Java’s null

Friday, June 7, 13 17
Friday, June 7, 13 18
exercise 4

Rewrite bake-cake using the


new add function.

Friday, June 7, 13 18
Friday, June 7, 13 19
exercise 5

Write a function scooped? which returns true


if the given ingredient (the argument) needs
scooping. Otherwise it returns false.

(scooped? :egg) => false


(scooped? :sugar) => true

Friday, June 7, 13 19
Friday, June 7, 13 20
exercise 6

Write a function squeezed? which returns


true if the given ingredient (the argument)
needs squeezing. Otherwise it returns false.

(squeezed? :egg) => true


(squeezed? :flour) => false

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.

(simple? :egg) => false


(simple? :butter) => true

Friday, June 7, 13 21
Friday, June 7, 13 22
if

Friday, June 7, 13 22
if

(if (scooped? :flour)


“makes sense”
“that’s weird”)

Friday, June 7, 13 22
if
condition

(if (scooped? :flour)


“makes sense”
“that’s weird”)

Friday, June 7, 13 22
if
condition

(if (scooped? :flour)


“makes sense” then expression
“that’s weird”)

Friday, June 7, 13 22
if
condition

(if (scooped? :flour)


“makes sense” then expression
“that’s weird”) else expression

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.

You will need to use if and do.

(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

Write the following functions using dotimes.

add-flour-cups
add-sugar-cups
add-milk-cups
add-butters

Friday, June 7, 13 26
Friday, June 7, 13 27
exercise 10

Rewrite bake-cake to use the new functions.

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

Rewrite add to be variadic and to


use the new add- functions.

Friday, June 7, 13 29
Friday, June 7, 13 30
exercise 12

Rewrite bake-cake to use the new add


function.

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

mix all ingredients


bake in pan for 25 minutes
let cool

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

You might also like