Skip to content

Instantly share code, notes, and snippets.

@kseo
Created March 23, 2016 12:02
Show Gist options
  • Save kseo/638ad8d87ecb23a1895b to your computer and use it in GitHub Desktop.
Save kseo/638ad8d87ecb23a1895b to your computer and use it in GitHub Desktop.
for loop in Haskell
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