Created
December 21, 2015 10:44
-
-
Save tatsuya6502/12ae8f298b448d648a51 to your computer and use it in GitHub Desktop.
MyMath, functions that are traced by ReconTrace
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
defmodule MyMath do | |
@spec factorial(non_neg_integer()) :: non_neg_integer() | |
def factorial(0), do: 1 | |
def factorial(n) when n > 0, do: n * factorial(n - 1) | |
@spec factorial_iter(non_neg_integer()) :: non_neg_integer() | |
def factorial_iter(n) when n >= 0, do: factorial_iter1(n, 1) | |
defp factorial_iter1(0, acc), do: 1 # `1` should read `acc` | |
defp factorial_iter1(n, acc), do: factorial_iter1(n - 1, acc * n) | |
@spec permutations([term()]) :: [[term()]] | |
def permutations([]) do | |
[[]] | |
end | |
def permutations(items) when is_list(items) do | |
for head <- items, tail <- permutations(items -- [head]) do | |
[head | tail] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment