[Haskell-cafe] Mysterious monads

Nicolas Frisby nicolas.frisby at gmail.com
Mon May 28 20:54:42 EDT 2007


On 5/27/07, Andrew Coppin <andrewcoppin at btinternet.com> wrote:
[snip]

> such that a Reader is created with an initial list, and the read
> function fetches 1 element out of that list. That is, the expression "x
> <- read" will take the head element of the list and put it into x,
> keeping the tail to be read later.
>

Your intended behavior for Reader indicates stateful computational
features. The "read later" roughly expands to "be read by some monadic
action on the rhs of a >>=" as in

(read >>= \x -> read {-this  is later-} >>= ...)

Recognizing the stateful nature gives you two options:

1) Implement yours as a restricted version of Control.Monad.State

    type ReaderAC = State
    readAC = get >>= \x -> put (tail x) >> return (head x)

2) or (as Isaac demonstrated) look to the definition of
Control.Monad.State.State for guidance own how to structure your
monad.


More information about the Haskell-Cafe mailing list