Skip to content

Instantly share code, notes, and snippets.

@aarroyoc
Created April 9, 2018 15:17
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 aarroyoc/a5b4f01083257ecdcc6394d13c6a35a0 to your computer and use it in GitHub Desktop.
Save aarroyoc/a5b4f01083257ecdcc6394d13c6a35a0 to your computer and use it in GitHub Desktop.
Suma con mónada ST
import Data.STRef
import Control.Monad
import Control.Monad.ST
sumST :: Num a => [a] -> a
sumST xs = runST $ do
n <- newSTRef 0
forM_ xs $ \x -> do
modifySTRef n (+x)
readSTRef n
main :: IO ()
main = do
putStrLn "Suma imperativa"
let x = sumST [1,2,3,4,5,6,7,8,9]
putStrLn ("Suma: "++(show x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment