Skip to content

Instantly share code, notes, and snippets.

@takeouchida
Created February 26, 2012 15:33
Show Gist options
  • Save takeouchida/1917408 to your computer and use it in GitHub Desktop.
Save takeouchida/1917408 to your computer and use it in GitHub Desktop.
A code snippet using Strict State Monad
_Main_mainzuzdszdwa_info:
Lc2q2:
addq $40,%r12
cmpq 144(%r13),%r12
ja Lc2q7
cmpq $100,%r14
jg Lc2qb
movq %rsi,%rax
addq %r14,%rax
incq %r14
movq %rax,%rsi
addq $-40,%r12
jmp _Main_mainzuzdszdwa_info
Main.main_$s$wa =
\ (sc_s2fe :: GHC.Prim.Int#)
(sc1_s2ff :: GHC.Prim.Int#)
(sc2_s2fg :: GHC.Types.Int) ->
case GHC.Prim.># sc_s2fe 100 of _ {
GHC.Types.False ->
Main.main_$s$wa
(GHC.Prim.+# sc_s2fe 1) (GHC.Prim.+# sc1_s2ff sc_s2fe) sc2_s2fg;
GHC.Types.True ->
(# GHC.Tuple.(), Main.MyState (GHC.Types.I# sc1_s2ff) sc2_s2fg #)
}
end Rec }
{-# LANGUAGE BangPatterns #-}
module Main (main) where
import Control.Monad.State.Strict
data MyState = MyState !Int !Int deriving Show
main :: IO ()
main = print $ execState (f 1) (MyState 0 0)
f :: Int -> State MyState ()
f n | n > 100 = return ()
| otherwise = modify' (\(MyState s t) -> MyState (s + n) t) >> f (n + 1)
modify' :: (MonadState s m) => (s -> s) -> m ()
modify' f = do
s <- get
put $! f s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment