Skip to content

Instantly share code, notes, and snippets.

@samof76
Created August 14, 2018 13:41
Show Gist options
  • Save samof76/e93e24d24d58a2b4313b509ef0ad056e to your computer and use it in GitHub Desktop.
Save samof76/e93e24d24d58a2b4313b509ef0ad056e to your computer and use it in GitHub Desktop.
Fibonacci List in Elixir
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