Guest User

Lazy/Strict State/IO monads

a guest
Nov 3rd, 2014
167
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Control.Applicative
  2. import Control.Monad.State.Lazy as Lazy
  3. import Control.Monad.State.Strict as Strict
  4. import Data.List
  5.  
  6. type LazyState     = Lazy.State Int
  7. type LazyStateIO   = Lazy.StateT Int IO
  8. type StrictState   = Strict.State Int
  9. type StrictStateIO = Strict.StateT Int IO
  10.  
  11. -- (1) exhausts the stack
  12. main1 = do
  13.   let x = Strict.evalState (head <$> mapM return [1..]) 0
  14.   print x
  15.  
  16. -- (2) exhausts the stack
  17. main2 = do
  18.   x <- Strict.evalStateT (head <$> mapM return [1..]) 0
  19.   print x
  20.  
  21. -- (3) works ok (constant size)
  22. main = do
  23.   let x = Lazy.evalState (head <$> mapM return [1..]) 0
  24.   print x
  25.  
  26. -- (4) exhausts the stack
  27. main4 = do
  28.   x <- Lazy.evalStateT (head <$> mapM return [1..]) 0
  29.   print x
RAW Paste Data