Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
fibonacci :: Integer -> Integer
fibonacci 0 = 0
fibonacci 1 = 1
fibonacci (-1) = 1
fibonacci (-2) = (-1)
fibonacci n | n > 0 = helper (fibonacci 0) (fibonacci 1) (n - 2)
| n < 0 = helper (fibonacci (-1)) (fibonacci (-2)) (n + 3)
helper :: Integer -> Integer -> Integer -> Integer
helper a b n | n == 0 && a > b = a - b
| n == 0 && a <= b = a + b
| n == (-1) = b - (a - b)
| n < 0 = helper b (a - b) (n + 1)
| n > 0 = helper b (a + b) (n - 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment