From c1e56f5c29a07469cd0448a4cfa03d09a939acf1 Mon Sep 17 00:00:00 2001 From: Oli Clive-Griffin Date: Thu, 3 Jun 2021 23:17:08 +1200 Subject: [PATCH] finished up to 14_recursion --- src/koans/13_creating_functions.clj | 16 ++++++++-------- src/koans/14_recursion.clj | 20 ++++++++++++++------ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/koans/13_creating_functions.clj b/src/koans/13_creating_functions.clj index 7d84bc8..35910bd 100644 --- a/src/koans/13_creating_functions.clj +++ b/src/koans/13_creating_functions.clj @@ -5,31 +5,31 @@ (meditations "One may know what they seek by knowing what they do not seek" - (= [__ __ __] (let [not-a-symbol? (complement symbol?)] + (= [true false true] (let [not-a-symbol? (complement symbol?)] (map not-a-symbol? [:a 'b "c"]))) "Praise and 'complement' may help you separate the wheat from the chaff" (= [:wheat "wheat" 'wheat] - (let [not-nil? ___] + (let [not-nil? (complement nil?)] (filter not-nil? [nil :wheat nil "wheat" nil 'wheat nil]))) "Partial functions allow procrastination" (= 20 (let [multiply-by-5 (partial * 5)] - (___ __))) + (multiply-by-5 4))) "Don't forget: first things first" - (= [__ __ __ __] + (= [:a :b :c :d] (let [ab-adder (partial concat [:a :b])] - (ab-adder [__ __]))) + (ab-adder [:c :d]))) "Functions can join forces as one 'composed' function" (= 25 (let [inc-and-square (comp square inc)] - (inc-and-square __))) + (inc-and-square 4))) "Have a go on a double dec-er" - (= __ (let [double-dec (comp dec dec)] + (= 8 (let [double-dec (comp dec dec)] (double-dec 10))) "Be careful about the order in which you mix your functions" - (= 99 (let [square-and-dec ___] + (= 99 (let [square-and-dec (comp dec square)] (square-and-dec 10)))) diff --git a/src/koans/14_recursion.clj b/src/koans/14_recursion.clj index ca29784..4c9d60f 100644 --- a/src/koans/14_recursion.clj +++ b/src/koans/14_recursion.clj @@ -3,21 +3,29 @@ (defn is-even? [n] (if (= n 0) - __ - (___ (is-even? (dec n))))) + true + (not (is-even? (dec n))))) (defn is-even-bigint? [n] (loop [n n acc true] (if (= n 0) - __ + acc (recur (dec n) (not acc))))) (defn recursive-reverse [coll] - __) - + (loop [coll coll + acc '()] + (if (empty? coll) + acc + (recur (rest coll) (conj acc (first coll)))))) + (defn factorial [n] - __) + (loop [n n + acc 1] + (if (= n 1) + acc + (recur (dec n) (* acc n))))) (meditations "Recursion ends with a base case"