Skip to content

Instantly share code, notes, and snippets.

@Rastrian
Created October 8, 2022 21:55
Show Gist options
  • Save Rastrian/f36d48db12fa969ec600b84c5bcf57cd to your computer and use it in GitHub Desktop.
Save Rastrian/f36d48db12fa969ec600b84c5bcf57cd to your computer and use it in GitHub Desktop.
OCaml Peano Fold
type nat = O | S of nat
let rec fold succ acc n =
match n with
| O -> acc
| S n' ->
let acc = succ acc in
fold succ acc n'
let add x y = fold (fun n -> S n) y x
let to_int = fold (fun n -> n + 1) 0
let two = S (S O)
let three = add (S O) two
let five = add three two
let () = Format.eprintf "%d\n%!" (to_int five)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment