Source

csci-350-lab1 / splitter.ml

print_endline("Splits a list into two based on divisibility by 2.");;

let rec fr ic l =
   try
      fr ic l@[(int_of_string (input_line ic))]
   with
      End_of_file -> l
;;

let rec splitter ll e o = 
   match ll with 
     [] -> e,o
   | h::t -> if (h mod 2) = 0 then (splitter t (h::e) o) else (splitter t e (h::o))
;;

let ic = open_in "input.dat" in
let ilist = fr ic [] in 
let e,o = splitter ilist [] [] in

print_endline ("Input: " ^ String.concat " " (List.map string_of_int ilist));
print_endline ("Evens: " ^ String.concat " " (List.map string_of_int e));
print_endline (" Odds: " ^ String.concat " " (List.map string_of_int o))