Skip to content

Instantly share code, notes, and snippets.

@Isweet
Created November 13, 2015 23:37
Show Gist options
  • Save Isweet/adf05037da204d118205 to your computer and use it in GitHub Desktop.
Save Isweet/adf05037da204d118205 to your computer and use it in GitHub Desktop.
A couple different Fibonacci's
import Data.Function.Memoize
-- SLOW
fib :: Int -> Integer
fib 0 = 0
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)
-- Memoized / FAST
fib' :: Int -> Integer
fib' = memoize h
where h 0 = 0
h 1 = 1
h n = fib' (n - 1) + fib' (n - 2)
fib'' :: Int -> Integer
fib'' = (map h [0..] !!)
where h 0 = 0
h 1 = 1
h n = fib'' (n - 1) + fib'' (n - 2)
main :: IO ()
main = putStrLn . show $ fib' 30000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment