elixir_channels_demo/lib/demo/funcs.ex

16 lines
343 B
Elixir
Raw Permalink Normal View History

2022-04-07 17:28:36 +00:00
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