Because they are more general functions that work on all monads rather
than just lists.

This allows Stream to be defined more flexibly.

On Mon, May 30, 2011 at 9:01 PM, John Ky <[email protected]> wrote:
> Hi all,
> I'm trying to learn about enumerators by reading this paper and came across
> some code on page 2 that I found hard to digest, but I think I finally got
> it:
>
> import Data.Monoid
> data Stream a
> = Chunks [a]
> | EOF
> deriving (Show, Eq)
> instance Monad Stream where
> return = Chunks . return
> Chunks xs >>= f = mconcat (fmap f xs)
> EOF >>= _ = EOF
> instance Monoid (Stream a) where
> mempty = Chunks mempty
> mappend (Chunks xs) (Chunks ys) = Chunks (xs ++ ys)
> mappend _ _ = EOF
>
> I guess, it shows my lack of experience in Haskell, but my question is, why
> is writing the code this way preferred over say writing it like this:
>
> import Data.Monoid
> data Stream a
> = Chunks [a]
> | EOF
> deriving (Show, Eq)
> instance Monad Stream where
> return x = Chunks [x]
> Chunks xs >>= f = mconcat (fmap f xs)
> EOF >>= _ = EOF
> instance Monoid (Stream a) where
> mempty = Chunks []
> mappend (Chunks xs) (Chunks ys) = Chunks (xs ++ ys)
> mappend _ _ = EOF
>
> Cheers,
> -John
>
> _______________________________________________
> Haskell-Cafe mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>

_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to