Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
type nat = Zero | Succ of nat
let rec int_of_nat x =
match x with
| Zero -> 0
| Succ x' -> 1 + int_of_nat x'
let rec nat_of_int x =
match x with
| 0 -> Zero
| _ -> Succ (nat_of_int (x-1))
let rec add x y =
match y with
| Zero -> x
| Succ y' -> add (Succ x) y'
let rec eq x y =
match (x, y) with
| (Zero, Zero) -> true
| (Zero, _) -> false
| (_, Zero) -> false
| (Succ x', Succ y') -> eq x' y'
let rec even x =
match x with
| Zero -> true
| Succ x' -> odd x'
and odd x =
match x with
| Zero -> false
| Succ x' -> even x'
let rec range x y =
if eq x y then
[]
else
x :: range (add x (Succ Zero)) y
let _ =
let a = nat_of_int (int_of_string (Sys.argv.(1))) in
let b = nat_of_int (int_of_string (Sys.argv.(2))) in
let list = range a b in
List.iter (fun x ->
if even x then
Printf.printf "%d " (int_of_nat x)
) list;
print_newline ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.