Advertisement
Guest User

Untitled

a guest
Aug 7th, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Control.Monad.ST
  2. import qualified Data.HashTable.ST.Basic as H
  3.  
  4. type HT s = H.HashTable s Int Int
  5.  
  6. fibCore :: Int -> Int -> HT s -> ST s Int
  7. fibCore n base cache = do
  8.   if (n == 1) || (n == 2)
  9.     then return 1
  10.     else do
  11.       u <- fibCoreCached (n - 1) base cache
  12.       v <- fibCoreCached (n - 2) base cache
  13.       return (mod (u + v) base)
  14.  
  15. fibCoreCached :: Int -> Int -> HT s -> ST s Int
  16. fibCoreCached n base cache = do
  17.   x <- H.lookup cache n
  18.   case x of
  19.     Just t -> return t
  20.     Nothing -> do
  21.       r <- fibCore n base cache
  22.       H.insert cache n r
  23.       return r
  24.  
  25. fib :: Int -> Int -> Int
  26. fib n base = runST $ do
  27.   cache <- H.new
  28.   fibCoreCached n base cache
  29.  
  30. main :: IO()
  31. main = putStrLn (show (fib 10000000 999983))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement