Skip to content

Instantly share code, notes, and snippets.

@Zeta611
Created August 9, 2022 12:55
Show Gist options
  • Save Zeta611/112bdd2fd669241ef3c9b23ce9104093 to your computer and use it in GitHub Desktop.
Save Zeta611/112bdd2fd669241ef3c9b23ce9104093 to your computer and use it in GitHub Desktop.
[Fixed-point combinator in OCaml] Fixed-point combinator (Z combinator) in OCaml. Y combinator is not available, as OCaml is a strict language. #demo
type 'a fix = Fix of ('a fix -> 'a)
let fix f =
(fun (Fix x as fix) -> f (fun z -> x fix z))
(Fix (fun (Fix x as fix) -> f (fun z -> x fix z)))
let factorial = fix @@ fun f n -> if n <= 0 then 1 else n * f (n - 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment