Last active
August 29, 2015 14:10
-
-
Save Arkar-Aung/1ee7039f3da2bbd11479 to your computer and use it in GitHub Desktop.
Solve calculating exponential in functional way without using loop, built-in multiply (*) and exponential function.
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 (calculator). | |
-export ([exponential/2, multiply/2]). | |
exponential(_, Exp) when(Exp < 0) -> | |
throw("Bad argument"); | |
exponential(Num, Exp) when (Exp > 0) -> | |
calc_exponential(Num, Num, Exp). | |
calc_exponential(_, _, 0) -> | |
1; | |
calc_exponential(Result, _, 1) -> | |
Result; | |
calc_exponential(Result, Num, Exp) -> | |
calc_exponential(multiply(Result, Num), Num, Exp - 1). | |
multiply(Num, Multiplier) -> | |
calc_multiply(0, Num, Multiplier). | |
calc_multiply(Result, _, 0) -> | |
Result; | |
calc_multiply(Result, Num, Multiplier) -> | |
calc_multiply(Result + Num, Num, Multiplier - 1). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment