let (<<) f g x = f (g x) let flip f x y = f y x let cumulate f init = let rec aux acc = function 0 -> acc | n -> aux (f acc n) (n - 1) in aux init let factorial = cumulate ( * ) 1 let power x = cumulate (fun acc _ -> acc * x) 1 let range = cumulate (fun acc n -> n - 1 :: acc) [] let repeat x = cumulate (fun acc _ -> x :: acc) [] let delete_at i = let rec aux j rs = function | x :: xs -> aux (j + 1) (x :: rs) xs in aux 0 [] let count f = let println_opt to_s = function | Some x -> println to_s x let f r m n = let x = factorial (r * m) / power (factorial m) r in let rec aux acc x n = function | a -> match delete_at i a with None, _ -> None | Some d, rest -> aux (d :: acc) x n rest in if n < 1 || x < n then None else aux [] x n a let () = g 9 1 1; g 9 1 2; g 9 1 3; g 9 1 123456; g 9 1 234567; g 9 1 362880; g 4 3 1; g 4 3 2; g 4 3 3; g 4 3 123456; g 4 3 234567; g 4 3 369600;
Standard input is empty
[1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 2, 3, 4, 5, 6, 7, 9, 8] [1, 2, 3, 4, 5, 6, 8, 7, 9] [4, 1, 6, 5, 8, 9, 7, 3, 2] [6, 8, 4, 7, 5, 3, 2, 1, 9] [9, 8, 7, 6, 5, 4, 3, 2, 1] [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4] [1, 1, 1, 2, 2, 2, 3, 3, 4, 3, 4, 4] [1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 3, 4] [2, 2, 2, 3, 3, 1, 4, 3, 4, 1, 1, 4] [3, 2, 4, 4, 2, 4, 3, 3, 1, 1, 1, 2] [4, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1, 1]