-
-
Save mecdemort/a5a7b994cdb3774e1843 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn accumulate1 | |
"accumulate a collection of terms from a to b using a combiner function" | |
([combiner init term a next b] | |
(->> (iterate next a) | |
(take-while #(<= % b) ,) | |
(map term ,) | |
(reduce combiner init ,)))) | |
(defn accumulate2 | |
"accumulate a collection of terms from a to b using a combiner function" | |
([combiner init term a next b] | |
(reduce combiner init | |
(for [x (iterate next a) | |
:while (<= x b)] | |
(term x))))) | |
(defn accumulate3 | |
"accumulate a collection of terms from a to b using a combiner function" | |
([combiner init term a next b] | |
(loop [a a, result init] | |
(if (> a b) | |
result | |
(recur (next a) (combiner result (term a))))))) | |
(defn summation | |
"sum f(n) from 0 to n-1" | |
([f n] (accumulate1 + 0 f 0 inc (dec n)))) | |
(defn e**x [x n] | |
(sumation #(/ (Math/pow x %) (factorial %)) n)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment