[back]

Fixpoints

Implement the function

val fix : ('a -> 'a) -> 'a -> 'a

so that fix f x is the result of applying f to x until the value doesn't change, and runs forever if this never occurs. Formally, fix f x is \(f^n(x)\) for any \(n\) such that \(f^n(x) = f^{n+1}(x)\), and is undefined if no such \(n\) exists. Your solution must be tail recursive.

Examples

let f x = if x * x > 100 then x else x * x
let _ = assert (fix f 3 = 81)