Main> :t modify ((+1) :: Int -> Int) modify (...) :: (MonadState Int a) => a ()This says that modify (+1) acts over any Monad that is a member of the MonadState class, with an Int state.
>>> import qualified Data.Vector as V >>> import qualified Data.Vector.Mutable as MV >>> V.modify (\v -> MV.write v 0 'x') $ V.replicate 4 'a' "xaaa"
>>> import qualified Data.Vector.Strict as V >>> import qualified Data.Vector.Strict.Mutable as MV >>> V.modify (\v -> MV.write v 0 'x') $ V.replicate 4 'a' "xaaa"
>>> import qualified Data.Vector.Primitive as VP >>> import qualified Data.Vector.Primitive.Mutable as MVP >>> VP.modify (\v -> MVP.write v 0 'x') $ VP.replicate 4 'a' "xaaa"
>>> import qualified Data.Vector.Storable as VS >>> import qualified Data.Vector.Storable.Mutable as MVS >>> VS.modify (\v -> MVS.write v 0 'x') $ VS.replicate 4 'a' "xaaa"
>>> import qualified Data.Vector.Unboxed as VU >>> import qualified Data.Vector.Unboxed.Mutable as MVU >>> VU.modify (\v -> MVU.write v 0 'x') $ VU.replicate 4 'a' "xaaa"
ref <- newIORef 0 replicateM_ 1000000 $ modifyIORef ref (+1) readIORef ref >>= printTo avoid this problem, use modifyIORef' instead.
>>> :{ runST (do ref <- newSTRef "" modifySTRef ref (const "world") modifySTRef ref (++ "!") modifySTRef ref ("Hello, " ++) readSTRef ref ) :} "Hello, world!"Be warned that modifySTRef does not apply the function strictly. This means if the program calls modifySTRef many times, but seldom uses the value, thunks will pile up in memory resulting in a space leak. This is a common mistake made when using an STRef as a counter. For example, the following will leak memory and may produce a stack overflow:
>>> import GHC.Internal.Control.Monad (replicateM_) >>> :{ print (runST (do ref <- newSTRef 0 replicateM_ 1000 $ modifySTRef ref (+1) readSTRef ref )) :} 1000To avoid this problem, use modifySTRef' instead.