2.2. Function Practice
We can check if two values are equal using =
.
These practice problems gradually get more difficult.
(= 4
(+ 2 2))
Modify the following code examples, replacing the underscores with values,
so the editor prints true
.
(= 10
(+ 3 _))
(= 30
(* 5 _))
(= _
(/ 36 9))
(= _
(* 10
(+ 3 4)))
(defn times-five
[x]
(* 5 x))
(= _
(+ (times-five 4)
5))
Hint: there's nothing stopping you from using a value multiple times.
(defn square
[x]
(* _ _))
(= 9
(square 3))
Here's a new concept: we can define a value outside our function definition. We can still use that within our function, though.
(def pi 3.14)
(defn circle-circumference
[radius]
(* 2 _ _))
(= 12.56
(circle-circumference 2))