roh*_*ant 3 haskell lazy-evaluation state-monad
import Control.Monad.State.Lazy
type Queue a = [a]
push :: a -> State (Queue a) ()
push x = state (\xs -> ((),xs++[x]))
pop :: State (Queue a) a
pop = state (\(x:xs) -> (x,xs))
queueManip :: State (Queue Int) Int
queueManip =
do
mapM_ push [1..]
a <- pop
return a
main :: IO()
main = do
let (a,_) = runState queueManip []
print a
Run Code Online (Sandbox Code Playgroud)
不mapM_应该懒惰吗?除了实现队列不应该复杂O(1)吗?
因为追加(++)本身就是懒惰......
如果我是邪恶的并且使用怎么办?
push' :: Int -> State (Queue Int) ()
push' 1052602983 = state $ \_ -> ((), []) -- "Muarhar!"
push' x = state $ \xs -> ((),xs++[x])
Run Code Online (Sandbox Code Playgroud)
然后mapM push' [1..]应该明确地将状态呈现为[1052602983, 1052602984 ..].pop屈服是错误的1.但是mapM,如果不先评估十亿其他数字,就不可能知道这一点.实际上将它们推向状态在这里是无关紧要的,并且push在完成monadic程序流程之前mapM,至少必须给它机会检查任何给定的数字也无关紧要.