Skip to content

Instantly share code, notes, and snippets.

@takei-shg
Created September 4, 2013 07:15
Show Gist options
  • Save takei-shg/6433657 to your computer and use it in GitHub Desktop.
Save takei-shg/6433657 to your computer and use it in GitHub Desktop.
fibs - Haskell
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