Skip to content

Instantly share code, notes, and snippets.

@gnuvince
Created June 13, 2012 02:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnuvince/2921431 to your computer and use it in GitHub Desktop.
Save gnuvince/2921431 to your computer and use it in GitHub Desktop.
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