Created
October 8, 2022 21:55
-
-
Save Rastrian/f36d48db12fa969ec600b84c5bcf57cd to your computer and use it in GitHub Desktop.
OCaml Peano Fold
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
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
https://en.wikipedia.org/wiki/Set-theoretic_definition_of_natural_numbers
https://en.wikipedia.org/wiki/Peano_axioms