Ticket #1376: StateAccum.hs

File StateAccum.hs, 503 bytes (added by logancapaldo@…, 7 years ago)

StateAccum.hs

Line 
1import Control.Monad
2import Control.Monad.State
3import Control.Monad.List
4
5sumUp :: [Int] -> State Int [()]
6sumUp = sequence . map (modify . (+))
7
8type StateWithList = ListT (State Int)
9
10sumUp' xs = do x <- xs
11               lift (modify (+x))
12               return (lift get)
13
14
15sumThreeLists :: [Int] -> [Int] -> [Int] -> Int
16sumThreeLists xs ys zs = execState (sumUp xs >> sumUp ys >> sumUp zs) 0
17
18main = do print $ execState (sumUp [1,2,3]) 0
19          print $ sumThreeLists [1..10] [1..10] [1..10]
20