Skip to content

Instantly share code, notes, and snippets.

@palladin
Created July 15, 2011 16:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save palladin/1085035 to your computer and use it in GitHub Desktop.
Save palladin/1085035 to your computer and use it in GitHub Desktop.
A Lazy fixed-point combinator
// x = f(x) encoded in F#
let force (value : Lazy<_>) = value.Force()
let fix f = let rec x = lazy (f x) in x
// Examples
let fac = fix (fun f x -> if x = 0 then 1 else x * force f (x - 1) )
let nums = fix (fun v -> seq { yield 0; yield! Seq.map ((+) 1) (force v) })
force fac 10 // 10! = 3628800
Seq.take 10 (force nums) // seq [0; 1; 2; 3; ...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment