Basically I have a function of the type
'a -> 'a (an optimization function on a AST) and I want to call it (passing the previous result) until it returns the same thing as the input.
Right now I have this:
let performOptimizations ast = Seq.initInfinite (fun _ -> 0) |> Seq.scan (fun last _ -> optimizeAst last) ast |> Seq.pairwise |> Seq.find (fun (first, second) -> first = second) |> fst
This seems really convoluted because it is created an infinite sequence of
ints just so that the scan will continue until the
ast is no longer modified. I realized that I could also do something like this:
let performOptimizations ast = let mutable newAst = optimizeAst ast let mutable lastAst = ast while newAst <> lastAst do lastAst <- newAst newAst <- optimizeAst newAst newAst
However, I would prefer to avoid mutation.
What's the functional, idiomatic way of doing this in F#?