Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fd35d2
commit 11b63ee
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
;; classic recursion (watch this one, it blows stack) | ||
(defn exp-r [x n] | ||
(if (zero? n) 1 | ||
(* x (exp-r x (dec n))))) | ||
|
||
|
||
;; tail recursion | ||
(defn exp-t [x n] | ||
(loop [acc 1 n n] | ||
(if (zero? n) acc | ||
(recur (* x acc) (dec n))))) | ||
|
||
|
||
;; functional | ||
(defn exp-f [x n] | ||
(reduce * (repeat n x))) | ||
|
||
;; sneaky (also blows stack, but not so easily) | ||
(defn exp-s [x n] | ||
(let [square (fn[x] (* x x))] | ||
(cond (zero? n) 1 | ||
(even? n) (square (exp-s x (/ n 2))) | ||
:else (* x (exp-s x (dec n)))))) | ||
|
||
;; library | ||
(require 'clojure.contrib.math) | ||
|
||
;;self-consitency test | ||
(let [flist (list exp-r exp-t exp-f clojure.contrib.math/expt exp-s)] | ||
(every? identity | ||
(map #(apply = %) | ||
(partition (count flist) | ||
(for [i (list 0 1 10 100 1000 4321) | ||
j (list 0 1 10 100 1000 4321) | ||
f flist ] | ||
(f i j)))))) |