RosettaCodeData/Task/Fibonacci-sequence/Lean/fibonacci-sequence-1.lean

20 lines
445 B
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Our first implementation is the usual recursive definition:
def fib1 :
| 0 := 0
| 1 := 1
| (n + 2) := fib1 n + fib1 (n + 1)
-- We can give a second more efficient implementation using an auxiliary function:
def fib_aux :
| 0 a b := b
| (n + 1) a b := fib_aux n (a + b) a
def fib2 :
| n := fib_aux n 1 0
-- Use #eval to check computations:
#eval fib1 20
#eval fib2 20