Created
July 15, 2011 16:36
-
-
Save palladin/1085035 to your computer and use it in GitHub Desktop.
A Lazy fixed-point combinator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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