Skip to content

Instantly share code, notes, and snippets.

@henrytill
Created March 28, 2017 20:53
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrytill/1d06b5171e3bc0c01f23af43275c0130 to your computer and use it in GitHub Desktop.
Save henrytill/1d06b5171e3bc0c01f23af43275c0130 to your computer and use it in GitHub Desktop.
Landin's Knot
(** "Landin's Knot" - implements recursion by backpatching *)
let landins_knot f =
let r = ref (fun x -> assert false) in
let fixedpoint = f (fun x -> !r x) in
r := fixedpoint;
fixedpoint
let factorial =
let g f x =
if x = 0 then
1
else
x * f (x - 1)
in
landins_knot g
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment