Skip to content

Instantly share code, notes, and snippets.

@intv0id
Created November 7, 2019 16:52
Show Gist options
  • Save intv0id/17ace0dc87d75c00eaf5c0f0997d89ac to your computer and use it in GitHub Desktop.
Save intv0id/17ace0dc87d75c00eaf5c0f0997d89ac to your computer and use it in GitHub Desktop.
let rec combinations_tail f k m prefix = match k, m with
| a, b when a < 0 || b < 0 -> failwith "ki or mi negative"
| a, b when a > b -> failwith "k > m"
| 0, 0 -> if f (Array.of_list prefix) then [prefix] else []
| 0, b -> combinations_tail f 0 (b-1) (false::prefix)
| a, b when a = b -> combinations_tail f (a-1) (b-1) (true::prefix)
| a, b -> (combinations_tail f (a-1) (b-1) (true::prefix)) @ (combinations_tail f a (b-1) (false::prefix))
;;
let generate_tail m n =
combinations_tail (sanity_check m n) ((m-1) * (n-1)) (m*(n-1)+n*(m-1)) []
;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment