Name: Anonymous 2008-06-17 17:37
the white man's burden of programming languages.
>>= takes a monad and a function, executes the monad with the function and packs the result into another monad.>>= returns the monad that results of applying the argument function to the argument monad's value (you can tell by >>='s type: Monad m => m a -> (a -> m b) -> m b). The argument monad will only be actually executed when the result monad is executed.
runIdentity (return 42).
assemblyLine w = (return w) >>= makeChopsticks >>= polishChopsticks >>= wrapChopsticksassemblyLine w = (return w)Maybe monad being able to handle lack of arguments by Nothing).>>= and return, and the way they are implemented must satisfy the following rules (1) return a >>= f = f a (left identity), (2) m >>= return = m (right identity), (3) (m >>= f) >>= g = m >>= (\x -> f x >>= g) (associativity). These allow you to reason more straight-forwardly about monadic code. (>>=) :: forall a b . m a -> (a -> m b) -> m b and return :: a -> m a. Other than that, what your Monad instances do is irrelevant. They can hold state (State, Reader), or no state ([a] (list monad), Maybe), produce side effects (IO, STM), provide continuation facilities (Cont), and so forth. The implementations of particular monads are not relevant to what the concept of Monads is, which is abstracting a pattern of computation and generalising it, thanks to type classes. There is nothing more to it.
instance Monad [])instance Monad Maybe where
(Just x) >>= k = k x
Nothing >>= _ = Nothing
(Just _) >> k = k
Nothing >> _ = Nothing
return = Just
fail _ = Nothing
do { ...; ... <- ...; ...}, mplus). That is all.
main = do
mapM_ putStrLn ["hey","guise"]withdraw :: Account -> Int -> STM ()
withdraw acc amount
= do { bal <- readTVar acc
; writeTVar acc (bal - amount) }parens :: Parser ()
parens = do{ char '('
; parens
; char ')'
; parens
}
<|> return ()