2020-01-02 17:59:27 +00:00
|
|
|
(def reduce
|
|
|
|
(fn (f xs initial-value)
|
|
|
|
(cond
|
|
|
|
((first xs) (reduce f (rest xs) (f initial-value (first xs))))
|
|
|
|
(true initial-value))))
|
|
|
|
|
|
|
|
(def map
|
|
|
|
(fn (f xs)
|
|
|
|
(reduce (fn (acc x) (cons acc (f x))) xs (list))))
|
|
|
|
|
|
|
|
(def range-list
|
|
|
|
(fn (x y init)
|
|
|
|
(if (< y x)
|
|
|
|
(do
|
|
|
|
(conj (range-list x (+ y 1) init) y))
|
|
|
|
init)))
|
|
|
|
(def range
|
|
|
|
(fn (x)
|
|
|
|
(range-list x 0 (list))))
|
|
|
|
|
2020-01-02 17:25:33 +00:00
|
|
|
(def fib
|
|
|
|
(fn (n)
|
|
|
|
(if (< n 2)
|
|
|
|
1
|
|
|
|
(+ (fib (- n 1))
|
|
|
|
(fib (- n 2))))))
|
|
|
|
|
2020-01-02 17:59:27 +00:00
|
|
|
|
|
|
|
(def benchmark-fn
|
|
|
|
(fn (x)
|
|
|
|
(let ((start (now)))
|
|
|
|
(fib x)
|
|
|
|
(- (now) start))))
|
|
|
|
|
|
|
|
|
|
|
|
(def run-benchmark
|
|
|
|
(fn (times)
|
|
|
|
(map (fn (x)
|
|
|
|
(println "Benchmark: " x)
|
|
|
|
(println "Took: " (benchmark-fn 30)))
|
|
|
|
(range times))))
|
|
|
|
|
|
|
|
|
|
|
|
(run-benchmark 20)
|