Skip to content

Instantly share code, notes, and snippets.

@tiensonqin
Created February 20, 2014 02:33
Show Gist options
  • Save tiensonqin/9106017 to your computer and use it in GitHub Desktop.
Save tiensonqin/9106017 to your computer and use it in GitHub Desktop.
-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