Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. import Control.Monad
  2. import Control.Monad.ST
  3. import Data.STRef
  4. import System.Environment (getArgs)
  5.  
  6. main :: IO ()
  7. main = do
  8. n <- fmap (read . head) $ getArgs
  9. putStrLn . show $ f n
  10.  
  11. f :: Int -> Integer
  12. f n = runST $ do
  13. first <- newSTRef 0
  14. second <- newSTRef 1
  15. loop n first second
  16. readSTRef second
  17.  
  18. loop n first second =
  19. if n == 0
  20. then
  21. return ()
  22. else do
  23. oldFirst <- readSTRef first
  24. oldSecond <- readSTRef second
  25. writeSTRef second $! oldFirst + oldSecond
  26. writeSTRef first oldSecond
  27. loop (n-1) first second
  28.  
  29. -- $ time ./ex2 1000000
  30. -- real 0m3.760s
  31. -- user 0m3.659s
  32. -- sys 0m0.071s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement