Finished koan 14

This commit is contained in:
Matt Davidson 2022-11-08 23:37:41 +00:00
parent 0d83f43cfd
commit 37bf3d22bf
4 changed files with 15 additions and 18 deletions

File diff suppressed because one or more lines are too long

View file

@ -3,7 +3,7 @@
(meditations
"We shall contemplate truth by testing reality, via equality"
(= true true)
(= true true)
"To understand reality, we must compare our expectations against reality"
(= 2 (+ 1 1))

View file

@ -29,8 +29,7 @@
(= 9 (((fn [] +)) 4 5))
"Functions can also take other functions as input"
(= 20 ((fn [f] (f 4 5))
*))
(= 20 ((fn [f] (f 4 5)) *))
"Higher-order functions take function arguments"
(= 25 ((fn [f] (f 5)) (fn [n] (* n n))))

View file

@ -1,23 +1,21 @@
(ns koans.14-recursion
(:require [koan-engine.core :refer :all]))
(defn is-even? [n]
(if (= n 0)
__
(___ (is-even? (dec n)))))
(defn is-even? [n]
(if (= n 0) true (not (is-even? (dec n)))))
(defn is-even-bigint? [n]
(loop [n n
acc true]
(if (= n 0)
__
(recur (dec n) (not acc)))))
(defn is-even-bigint? [n] (loop [n n acc true]
(if (= n 0) acc (recur (dec n) (not acc)))))
(defn recursive-reverse [coll]
__)
(defn recursive-reverse [coll]
(loop [coll coll newlist ()]
(if (seq coll)
(recur (rest coll) (conj newlist (first coll)))
newlist)))
(defn factorial [n]
__)
(defn factorial [n]
(loop [n n acc 1]
(if (= n 0) acc (recur (dec n) (* n acc)))))
(meditations
"Recursion ends with a base case"