Created
September 4, 2013 07:15
-
-
Save takei-shg/6433657 to your computer and use it in GitHub Desktop.
fibs - Haskell
This file contains 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_slow :: Int -> Int | |
fib_slow 0 = 1 | |
fib_slow 1 = 1 | |
fib_slow n = fib_slow (n - 2) + fib_slow (n - 1) | |
fibs_slow :: Int -> [Int] | |
fibs_slow len = take len $ map fib_slow [0..] | |
fib :: Int -> Int | |
fib 0 = 1 | |
fib 1 = 1 | |
fib n = fib' n 1 1 | |
fib' :: Int -> Int -> Int -> Int | |
fib' 1 _ acc = acc | |
fib' n x succ = fib' (n-1) succ (x + succ) | |
-- this causes overflow | |
fibs :: Int -> [Int] | |
fibs len = take len $ map fib [0..] | |
-- this causes overflow | |
fibs_idx :: Int -> Int | |
fibs_idx idx = map fib [0..] !! idx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment