Conditionals & Structures

Question 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)

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

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

Question 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)))

Question 4: Reimplement message using the if structure.

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

Question 5: Reimplement message using the cond structure.

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

Question 6: Reimplement message using the case structure.

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

Question 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))))