class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b
(>>) :: m a -> m b -> m b
x >> y = x >>= \\_ -> y
fail :: String -> m a
fail msg = error msg
= 함수 (=bind)
함수
instance Monad Maybe where
return x = Just x
Nothing >>= f = Nothing
Just x >>= f = f x
fail _ = Nothing
Do
notation= 로 이어붙이는 것과 동일한 효과
ghci> Just 9 >>= (\\x -> Just (x > 8))
Just True
-- 동일한 표현식
marySue :: Maybe Bool
marySue = do
x <- Just 9
Just (x > 8)
wopwop :: Maybe Char
wopwop = do
(x:xs) <- Just ""
return x
ghci> wopwop
Nothing
instance Monad [] where
return x = [x]
xs >>= f = concat (map f xs)
fail _ = []