let group_by cmp lst = let rec group_by' lst result = match lst with | [] -> result | x::xs -> let value = cmp x in let lst1, lst2 = List.partition (fun v -> value = cmp v) lst in group_by' lst2 ((value, lst1)::result) in group_by' lst [] let list_max_by f lst = let rec list_max_by' lst result = match lst with | [] -> result | x::xs -> let v = f x in let result = max result v in list_max_by' xs result in if (List.length lst) <= 0 then failwith "list_max_by" else list_max_by' (List.tl lst) (f (List.hd lst)) let solve a_lst = let g_lst = group_by (fun a -> a) a_lst in let g_cnt = List.length g_lst and max_cnt = List.map (fun (_, l) -> List.length l) g_lst |> list_max_by (fun a -> a) in match g_cnt, max_cnt with | 4, 2 -> "ONE PAIR" | 3, 2 -> "TWO PAIR" | 3, 3 -> "THREE CARD" | 2, 3 -> "FULL HOUSE" | _, _ -> "NO HAND" let () = let a_lst = read_line () |> Str.split (Str.regexp_string " ") in solve a_lst |> print_endline