let rec all_arrangements n l =
assert (n > 0);
match n with
| 1 -> List.map (fun x -> [x]) l
| _ ->
List.fold_left (fun acc l' ->
List.fold_left (fun acc x -> (x :: l') :: acc) acc l
) [] (all_arrangements (n - 1) l)