[Haskell-cafe] question - which monad to use?

Tamas K Papp tpapp at Princeton.EDU
Sun Oct 1 18:00:43 EDT 2006


Hi,

I have a computation where a function is always applied to the
previous result.  However, this function may not return a value (it
involves finding a root numerically, and there may be no zero on the
interval).  The whole problem has a parameter c0, and the function is
also parametrized by the number of steps that have been taken
previously.

To make things concrete,

type Failmessage = Int          -- this might be something more complex
data Result a = Root a | Failure Failmessage -- guess I could use Either too

f :: Double -> Int -> Double 0 -> Result Double
f c0 0 _ = c0
f c0 j x = {- computation using x, parameters calculated from c0 and j -}

Then

c1 = f c0 0 c0
c2 = f c0 1 c1
c3 = f c0 2 c2
...

up to cn.

I would like to

1) stop the computation when a Failure occurs, and store that failure

2) keep track of intermediate results up to the point of failure, ie
have a list [c1,c2,c3,...] at the end, which would go to cn in the
ideal case of no failure.

I think that a monad would be the cleanest way to do this.  I think I
could try writing one (it would be a good exercise, I haven't written
a monad before).  I would like to know if there is a predefined one
which would work.

Thank you,

Tamas


More information about the Haskell-Cafe mailing list