【发布时间】:2015-07-09 04:32:04
【问题描述】:
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
mapM_ 不应该偷懒吗?除了实现队列之外,复杂度不应该是O(1)吗?
因为附加 (++) 本身是惰性的...
【问题讨论】:
标签: haskell lazy-evaluation state-monad