import Control.Concurrent.STM import Control.Concurrent.STM.TVar --import Control.Monad.State reimplemented below for illustration import Data.Set zeroRepeats :: [Int] -> [Int] zeroRepeats xs = evalState (zeroRepeatsS xs) empty zeroRepeatsS :: [Int] -> State (Set Int) [Int] zeroRepeatsS [] = return [] zeroRepeatsS (x:xs) = do x' <- zeroIfRepeat x xs' <- zeroRepeatsS xs return (x':xs') where zeroIfRepeat :: Int -> State (Set Int) Int zeroIfRepeat x = do s <- get if member x s then return 0 else do put (insert x s) return x -- same as Control.Monad.State -- definition newtype State s a = State { runState :: s -> (a, s) } -- make it a monad instance Monad (State s) where return x = State $ \s -> (x, s) m >>= f = State $ \s -> let (x, s') = runState m s in runState (f x) s' -- primitives for flavor get :: State s s get = State $ \s -> (s, s) put :: s -> State s () put s = State $ \_ -> ((), s) -- a way to run actions evalState :: State s a -> s -> a evalState m s = fst $ runState m s -- In fact Control.Monad.State exposes its internals, since it's not -- providing any theorems, so get, put, evalState are not strictly necessary -- but exist for convenience.