Created
March 23, 2016 12:02
-
-
Save kseo/638ad8d87ecb23a1895b to your computer and use it in GitHub Desktop.
for loop in Haskell
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 | |
import Control.Monad.ST | |
import Data.STRef | |
sumST :: Num a => [a] -> a | |
sumST xs = runST $ do | |
n <- newSTRef 0 | |
forM_ xs $ \x -> modifySTRef n (+x) | |
readSTRef n | |
sum_ :: Num a => [a] -> a | |
sum_ = foldr (+) 0 | |
main = do | |
let xs = [1..10] | |
print $ sumST xs | |
print $ sum_ xs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment