04/10/15 04:13:08
>>242
> List モナドは非決定性をもつ計算の連鎖を、演算をそれぞれのステップで可能なすべての値に適用することで、合成する戦略を内包しています。
だけなら、モナドでなくてもいいね。
------------
module MList where
data MList a = a :*: (MList a) | MNull deriving (Eq, Show)
infixr 3 :*:
instance {- Broken -} Monad MList where
(x :*: xs) >>= f = merge (f x) (xs >>= f)
where
merge (x :*: xs) ys = x :*: merge ys xs
merge MNull ys = ys
MNull >>= f = MNull
return x = x :*: MNull
fail s = MNull
toMList xs = foldr (:*:) MNull xs
(>=>) = (>>=) -- 2ch : ">> が多すぎます!"
(>=>) :: Monad m => m a -> (a -> m b) -> m b
test f = test1 f == test2 f
test1 f = f [1,2] >=> \x -> f [3, 4] >=> \y -> f [5, 6]
test2 f = (f [1,2] >=> \x -> f [3, 4]) >=> \y -> f [5, 6]