Newbie qustion about monads

Marcin 'Qrczak' Kowalczyk qrczak at
Thu Oct 2 15:27:29 EDT 2003

W liście z czw, 02-10-2003, godz. 12:59, Juanma Barranquero pisze:

> It's not about counting the operations (that's just an example), but
> accumulating any kind of state. For example:
>   data Accum a = Ac [a] a
>   instance Monad Accum where
>       return x     = Ac [x] x
>       Ac _ x >>= f = let Ac l x' = f x in Ac (l ++ [x']) x'

Accumulating state is fine. These definitions don't accumulate state:
'return' should yield a "neutral" state, and the above ">>=" ignores the
state of the lhs.

The type of Accum makes little sense. Each atomic operation can have
a different result type, so in order to accumulate something the state
can't depend on the type of the result.

data Accum s a = Ac [s] a

instance Monad (Accum s) where
   return x      = Ac [] x
   Ac s1 x >>= f = let Ac s2 y = f x in Ac (s1++s2) y

output :: a -> Accum a ()
output x = Ac [x] ()

   __("<         Marcin Kowalczyk
   \__/       qrczak at

More information about the Haskell-Cafe mailing list