Created
November 27, 2012 13:33
-
-
Save notpushkin/4154240 to your computer and use it in GitHub Desktop.
partitions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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