Created
February 20, 2014 02:33
-
-
Save tiensonqin/9106017 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
-module(math). | |
-compile(export_all). | |
%% erlang version | |
pow(_X, 0) -> | |
1; | |
pow(X, N) -> | |
X * pow(X, N-1). | |
%% clojure version | |
%% non-TCO version | |
(defn pow | |
[x, n] | |
(if (= x 0) | |
1 | |
(* x (dec n)))) | |
%% TCO version | |
(defn pow | |
[x, n] | |
(loop [n n acc 1] | |
(if (= n 0) | |
acc | |
(recur (dec n) (*' x acc))))) | |
%% functional version | |
(defn pow | |
[x, n] | |
(reduce * (repeat n x))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment