Created
June 13, 2012 02:23
-
-
Save gnuvince/2921431 to your computer and use it in GitHub Desktop.
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 = 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