Skip to content

Instantly share code, notes, and snippets.

@notpushkin
Created November 27, 2012 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notpushkin/4154240 to your computer and use it in GitHub Desktop.
Save notpushkin/4154240 to your computer and use it in GitHub Desktop.
partitions
let rec range a b =
if a > b
then
[]
else
a :: (range (a+1) b);;
let print_list f lst =
print_string "[";
List.iter (fun x -> (f x; print_string "; ")) lst;
print_string "]";
print_newline ();;
let rec parts_by_num n b =
let _ = print_int n
and _ = print_newline ()
in
if n = 0
then
[[]]
else
List.flatten(
List.map
(fun x ->
List.map
(fun l -> x :: l)
(parts_by_num (n-x) x)
)
(range b n)
);;
print_list (print_list print_int) (parts_by_num 4 1);;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment