PageRenderTime 30ms CodeModel.GetById 20ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/fibonacci.exs

http://github.com/josevalim/elixir
Elixir | 19 lines | 16 code | 3 blank | 0 comment | 0 complexity | 0fa6578f95a0df08507002ba57e54a18 MD5 | raw file
 1% A tail call optimized fibonacci.
 2module Math
 3  def fibonacci(n)
 4    fibonacci(n, 1, 0)
 5  end
 6
 7  def fibonacci(0, _, result)
 8    result
 9  end
10
11  def fibonacci(n, next, result)
12    fibonacci(n - 1, next + result, next)
13  end
14end
15
16Math.fibonacci(0)   % => 0
17Math.fibonacci(1)   % => 1
18Math.fibonacci(3)   % => 2
19Math.fibonacci(10)  % => 55