John Smith
John Smith

Reputation: 55

Haskell different implementations of sequence_

I tested following implementations of

sequence_' :: Monad m => [m a] -> m () 

sequence_'b [] = return ()
sequence_'b (m : ms) = (foldl (>>) m ms) >> return ()

sequence_'d [] = return ()
sequence_'d (m : ms) = m >> sequence_'d ms

sequence_'e [] = return ()
sequence_'e (m : ms) = m >>= \ _ -> sequence_'e ms

sequence_'g ms = foldr (>>) (return ()) ms

To test these I used following:

sequence_'e [putChar 'a', putChar 'b', putChar 'c']

and

sequence_'e [getChar, getChar, getChar]

The above tests worked. The first one put the value abc on the HUGS screen and the cursor returned to the next line.

The second one accepted three characters typed on the HUGS screen and the cursor returned to the next line.

Please suggest more tests which I could do to test these implementations of sequence_ (I guess this is a sequence Monad).

Thanks

Upvotes: 0

Views: 143

Answers (1)

luqui
luqui

Reputation: 60503

It's usually good to check what happens with infinite lists in Haskell

sequence_ (repeat (putChar 'a'))

and with lazy monads like Writer

import Control.Monad.Writer
execWriter (sequence_ (repeat (tell "a")))

If my intuition is correct, you should see at least one difference between your implementations with these cases.

Oh, and just to correct your vocabulary. sequence is just a function (some would call it a "monadic function"). Here are some diagrams about where the actual monad is:

putChar :: Char -> IO   ()
                   ^^
                  monad

tell :: w -> Writer w   ()
             ^^^^^^^^
              monad

sequence_ :: (Monad m) => [m a] -> m ()
                    ^      ^       ^
                    +------+-------+
                          monad

Upvotes: 1

Related Questions