linsyking / elm-monad / Monad.Writer

Writer Monad

Computations which produce a stream of data in addition to the computed values.

Binding strategy: A Writer monad value is a (computation value, log value) pair. Binding replaces the computation value with the result of applying the bound function to the previous value and appends any log data from the computation to the existing log data.

Useful for: Logging, or other computations that produce output "on the side".

The definition of writer monad in Haskell is

newtype Writer w a = Writer { runWriter :: (a,w) }

where w should be monoid (with mappend function).

We assume you only use List so we replace w with List w


type Writer w a
    = Writer (( a, List w ))

Writer

return : a -> Writer w a

return function for Writer

runWriter : Writer w a -> ( a, List w )

Run the writer

bind : Writer w a -> (a -> Writer w b) -> Writer w b

bind function for Writer

tell : List w -> Writer w ()

tell function for Writer