Created
August 14, 2018 13:41
-
-
Save samof76/e93e24d24d58a2b4313b509ef0ad056e to your computer and use it in GitHub Desktop.
Fibonacci List in Elixir
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
defmodule Math do | |
def fib(n) when n == 1, do: [0] | |
def fib(n) when n > 1, do: fib([0], n-1) | |
def fib([0], n), do: fib([1|[0]], n-1) | |
def fib(l,0), do: Enum.reverse(l) | |
def fib(l,n) do | |
[h1|[h2 | _]] = l | |
fib([h1+h2 | l], n-1) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment