Last active
August 29, 2015 14:02
-
-
Save banacorn/d21bcb7f1d8f940afc11 to your computer and use it in GitHub Desktop.
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
import Control.Monad (forM_) | |
import Control.Monad.ST (runST) | |
import Data.IORef | |
import Data.STRef | |
-- dirty, can't get rid of IO | |
sumIO :: [Int] -> IO Int | |
sumIO xs = do | |
acc <- newIORef 0 | |
forM_ xs (modifyIORef acc . (+)) | |
readIORef acc | |
-- still a bit dirty, yet pure | |
sumST :: [Int] -> Int | |
sumST xs = runST $ do | |
acc <- newSTRef 0 | |
forM_ xs (modifySTRef acc . (+)) | |
readSTRef acc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment