[Haskell-cafe] Stacking monads
ryani.spam at gmail.com
Fri Oct 3 15:48:28 EDT 2008
On Fri, Oct 3, 2008 at 12:43 PM, Andrew Coppin
<andrewcoppin at btinternet.com> wrote:
> factorise n = do
> x <- [1..]
> y <- [1..]
> if x*y == n then return (x,y) else fail "not factors"
> This is a very stupid way to factorise an integer. (But it's also very
> general...) As you may already be aware, this fails miserably because it
> tries all possible values for y before trying even one new value for x. And
> since both lists there are infinite, this causes an endless loop that
> produces (almost) nothing.
You should look at LogicT at http://okmij.org/ftp/Computation/monads.html
The magic words you are looking for are "fair disjunction" and "fair
The paper is full of mind-stretching code but it already does
everything you want. And it is a monad transformer already, so it's
easy to attach Error to it.
More information about the Haskell-Cafe