[Haskell-cafe] iterative algorithms: how to do it in Haskell?
Tamas K Papp
tpapp at Princeton.EDU
Wed Aug 16 09:06:12 EDT 2006
Hi,
I am a newbie learning Haskell. I have used languages with functional
features before (R, Scheme) but not purely functional ones without
side-effects.
Most of the programming I do is numerical (I am an economist). I
would like to know how to implement the iterative algorithm below in
Haskell.
f is an a->a function, and there is a stopping rule
goOn(a,anext) :: a a -> Bool which determines when to stop. The
algorithm looks like this (in imperative pseudocode):
a = ainit
while (true) {
anext <- f(a)
if (goOn(a,anext))
a <- anext
else
stop and return anext
}
For example, f can be a contraction mapping and goOn a test based on
the metric. I don't know how to do this in a purely functional
language, especially if the object a is large and I would like it to
be garbage collected if the iteration goes on.
Thank you,
Tamas
More information about the Haskell-Cafe
mailing list