Skip to content

Instantly share code, notes, and snippets.

@nkapliev
Created February 25, 2016 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkapliev/f8e168b5720621630c9c to your computer and use it in GitHub Desktop.
Save nkapliev/f8e168b5720621630c9c to your computer and use it in GitHub Desktop.
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