Add solutions to section 08-conditionals and 09-higher-order-functions
This commit is contained in:
parent
a815ae70ff
commit
a212416ce2
2 changed files with 30 additions and 22 deletions
|
|
@ -1,37 +1,44 @@
|
|||
(defn explain-exercise-velocity [exercise-term]
|
||||
(case exercise-term
|
||||
:ten-mph :pretty-fast
|
||||
:watching-tv :not-so-fast))
|
||||
|
||||
|
||||
|
||||
; "You will face many decisions"
|
||||
(= __ (if (false? (= 4 5))
|
||||
(= :a (if (false? (= 4 5))
|
||||
:a
|
||||
:b))
|
||||
|
||||
; "Some of them leave you no alternative"
|
||||
(= __ (if (> 4 3)
|
||||
[]))
|
||||
(= [] (if (> 4 3) ;; J - This is the equivalent of an if statement without an else.
|
||||
[])) ;; returns nil if false as seen below.
|
||||
|
||||
; "And in such a situation you may have nothing"
|
||||
(= __ (if (nil? 0)
|
||||
(= nil (if (nil? 0)
|
||||
[:a :b :c]))
|
||||
|
||||
; "In others your alternative may be interesting"
|
||||
(= :glory (if (not (empty? ()))
|
||||
:doom
|
||||
__))
|
||||
:glory))
|
||||
|
||||
; "You may have a multitude of possible paths"
|
||||
(let [x 5]
|
||||
(= :your-road (cond (= x __) :road-not-taken
|
||||
(= x __) :another-road-not-taken
|
||||
:else __)))
|
||||
(= :your-road (cond (= x 3) :road-not-taken
|
||||
(= x 2) :another-road-not-taken
|
||||
:else :your-road))) ;; J - The syntax and formatting of this statement
|
||||
;; are crucial in writing these statements correctly.
|
||||
|
||||
; "Or your fate may be sealed"
|
||||
(= 'doom (if-not (zero? __)
|
||||
(= 'doom (if-not (zero? 1)
|
||||
'doom
|
||||
'more-doom))
|
||||
|
||||
; "In case of emergency, go fast"
|
||||
(= "pretty fast"
|
||||
(explain-exercise-velocity __))
|
||||
(= :pretty-fast
|
||||
(explain-exercise-velocity :ten-mph))
|
||||
|
||||
; "But admit it when you don't know what to do"
|
||||
(= __
|
||||
(= :not-so-fast
|
||||
(explain-exercise-velocity :watching-tv))
|
||||
|
|
|
|||
|
|
@ -1,32 +1,33 @@
|
|||
|
||||
; "The map function relates a sequence to another"
|
||||
(= [__ __ __] (map (fn [x] (* 4 x)) [1 2 3]))
|
||||
(= [4 8 12] (map (fn [x] (* 4 x)) [1 2 3]))
|
||||
|
||||
; "You may create that mapping"
|
||||
(= [1 4 9 16 25] (map (fn [x] __) [1 2 3 4 5]))
|
||||
(= [1 4 9 16 25] (map (fn [x] (* x x)) [1 2 3 4 5]))
|
||||
|
||||
; "Or use the names of existing functions"
|
||||
(= __ (map nil? [:a :b nil :c :d]))
|
||||
(= '(false false true false false) (map nil? [:a :b nil :c :d]))
|
||||
|
||||
; "A filter can be strong"
|
||||
(= __ (filter (fn [x] false) '(:anything :goes :here)))
|
||||
(= () (filter (fn [x] false) '(:anything :goes :here)))
|
||||
|
||||
; "Or very weak"
|
||||
(= __ (filter (fn [x] true) '(:anything :goes :here)))
|
||||
(= '(:anything :goes :here) (filter (fn [x] true) '(:anything :goes :here)))
|
||||
|
||||
; "Or somewhere in between"
|
||||
(= [10 20 30] (filter (fn [x] __) [10 20 30 40 50 60 70 80]))
|
||||
(= [10 20 30] (filter (fn [x] (<= x 30)) [10 20 30 40 50 60 70 80]))
|
||||
|
||||
; "Maps and filters may be combined"
|
||||
(= [10 20 30] (map (fn [x] __) (filter (fn [x] __) [1 2 3 4 5 6 7 8])))
|
||||
(= [10 20 30] (map (fn [x] (* x 10))
|
||||
(filter (fn [x] (<= x 3)) [1 2 3 4 5 6 7 8])))
|
||||
|
||||
; "Reducing can increase the result"
|
||||
(= __ (reduce (fn [a b] (* a b)) [1 2 3 4]))
|
||||
(= 24 (reduce (fn [a b] (* a b)) [1 2 3 4]))
|
||||
|
||||
; "You can start somewhere else"
|
||||
(= 2400 (reduce (fn [a b] (* a b)) __ [1 2 3 4]))
|
||||
(= 2400 (reduce (fn [a b] (* a b)) (cons 100 [1 2 3 4])))
|
||||
|
||||
; "Numbers are not the only things one can reduce"
|
||||
(= "longest" (reduce (fn [a b]
|
||||
(if (< __ __) b a))
|
||||
(if (< (count a)) b a))
|
||||
["which" "word" "is" "longest"]))
|
||||
|
|
|
|||
Loading…
Reference in a new issue