Edit me

Conditionals & Structures

Exercise 1: Use the let structure inside a function f1 to define a local variable b with the value "funcy". Then use the str function to combine two bs into "funcyfuncy".

(defn f1 []
  (let [b "funcy"]
    (str b b)))
(f1)

Exercise 2: Define a function small? that returns true for numbers under 100.

(defn small? [n] (< n 100))
(small? 99)  # true
(small? 100) # false

Exercise 3: Define a function message that has three cases:

(message :boink) # -> "Boink!"
(message :pig)   # -> "Oink!"
(message :ping)  # -> "Pong"
(defn message [k]
  (let [m {:boink "Boink!"
           :pig "Oink!"
           :ping "Pong"}]
    (get m k)))

Exercise 4: Reimplement message using the if structure.

(defn message [k]
  (if (= k :boink)
    "Boink!"
    (if (= k :pig)
      "Oink!"
      (if (= k :ping)
        "Pong!"))))

Exercise 5: Reimplement message using the cond structure.

(defn message [k]
  (cond
    (= k :boink) "Boink!"
    (= k :pig) "Oink!"
    (= k :ping) "Pong!"))

Exercise 6: Reimplement message using the case structure.

(defn message [k]
  (case k
    :boink "Boink!"
    :pig "Oink!"
    :ping "Pong!"))

Exercise 7: Use the loop structure to add 1 to an empty vector until it has 10 elements.

(loop [v []]
  (if (= (count v) 10)
       v
       (recur (push v 1))))