Skip to content

Instantly share code, notes, and snippets.

@banacorn
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banacorn/d21bcb7f1d8f940afc11 to your computer and use it in GitHub Desktop.
Save banacorn/d21bcb7f1d8f940afc11 to your computer and use it in GitHub Desktop.
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