Created
May 5, 2012 12:28
-
-
Save jprupp/2602003 to your computer and use it in GitHub Desktop.
Fibonacci in Haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fib :: Int -> Int | |
fib x | |
| x < 0 = error ("Can't calculate fib of negative number " ++ show x) | |
| x == 0 = 0 | |
| x > 0 = accum (x-1) 0 1 | |
where | |
accum :: Int -> Int -> Int -> Int | |
accum x y z | |
| x > 0 = accum (x-1) z (y+z) | |
| otherwise = z | |
main = print [(x,fib x) | x <- [0..9]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Flexing my functional muscle.