Created
April 9, 2018 15:17
-
-
Save aarroyoc/a5b4f01083257ecdcc6394d13c6a35a0 to your computer and use it in GitHub Desktop.
Suma con mónada ST
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 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