What's the most idiomatic way to call a function an arbitrary number of times with the previous result? Basically I have a function of the type 'a -&gt; '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 _ -&gt; 0)
  |&gt; Seq.scan (fun last _ -&gt; optimizeAst last) ast
  |&gt; Seq.pairwise
  |&gt; Seq.find (fun (first, second) -&gt; first = second)
  |&gt; 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 &lt;&gt; lastAst do
    lastAst &lt;- newAst
    newAst &lt;- optimizeAst newAst

  newAst


However, I would prefer to avoid mutation.

What's the functional, idiomatic way of doing this in F#?
I general, I agree with what mavnn said in his answer, I just think the function can be written in a simpler way:

let rec performOptimizations ast =
    let optimized = optimizeAst ast
    if ast = optimized then
        ast
    else
        performOptimizations optimized


This method also sounds like a good candidate for making it more generic, something like:

let rec doWhileNotSame seed getNextValue =
    let nextValue = getNextValue seed
    if seed = nextValue then
        seed
    else
        doWhileNotSame nextValue getNextValue