16 lines
343 B
Elixir
16 lines
343 B
Elixir
defmodule Funcs do
|
|
|
|
# Naive, slow and buggy implementation:
|
|
def fib(0), do: 0
|
|
def fib(1), do: 1
|
|
def fib(n), do: fib(n-1) + fib(n-2)
|
|
|
|
# Fast version:
|
|
# def fib(n) when not is_number(n) or n < 0, do: raise("arg!")
|
|
# def fib(n), do: fib(0, 1, n)
|
|
#
|
|
# def fib(a, b, 0), do: a
|
|
# def fib(a, b, n), do: fib(b, a+b, n-1)
|
|
|
|
end
|