Here is the solution for Clojure Koans on conditionals.
(defn explain-defcon-level [exercise-term]
(defn explain-defcon-level [exercise-term]
(case exercise-term
:fade-out :you-and-what-army
:double-take :call-me-when-its-important
:round-house :o-rly
:fast-pace :thats-pretty-bad
:cocked-pistol :sirens
:say-what?))
; note the case
construct
; I bet it is
not the same as it was in your previous language
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;1
(= __ (if (false? (= 4 5))
:a
:b))
; if in Clojure
has no then
; more
surprisingly there is no else
; although
there is :else
; false? is a
predicate
; since (false?
(= 4 5 )) is true
; the value of
if expression (yeah!)
; is :a
(= :a (if (false? (= 4 5))
:a
:b))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;2
(= __ (if (> 4 3)
[]))
; not much of a choice here 4 is > 3 so answer is []
(= [] (if (> 4 3)
[]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;3
(= __ (if (nil? 0)
[:a :b :c]))
; the if
condition is false
; because zero
is not nil, it is a int
; there is
nothing in the 'else' section
; so the
expression overall evaluates to nil
(= nil (if (nil? 0)
[:a :b :c]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;4
(= :glory (if (not (empty? ()))
:doom
__))
; :glory would
make them equal
(= :glory (if (not (empty? ()))
:doom
:glory))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;5
(let [x 5]
(= :your-road (cond (= x __) :road-not-taken
(= x __) :another-road-not-taken
:else __)))
; cond is a
macro using if
; for the first
2 conditions we put in values of x
; not equal to 5
; and put the
correct keyword in the :else part
(let [x 5]
(= :your-road (cond (= x 6) :road-not-taken
(= x 7) :another-road-not-taken
:else :your-road )))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;6
(= __ (if-not (zero? __)
'doom
'doom))
; since both
paths yield the same value of expression
; it does not
matter which one we take
; so both are
valid
(= 'doom (if-not (zero? 0)
'doom
'doom))
(= 'doom (if-not (zero? 1)
'doom
'doom))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;7
(= :sirens
(explain-defcon-level __))
; obviously
(= :sirens
(explain-defcon-level :cocked-pistol))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;8
(= __
(explain-defcon-level :yo-mama)))
; again look at
the mapping
(= :say-what?
(explain-defcon-level :yo-mama))
No comments:
Post a Comment