nCr コンビネーション
nCr = n-1Cr-1 + n-1Cr
n人からr人を選ぶ =
hさんを選び、残りのn-1人からr-1人を選ぶ => n-1Cr-1
+
hさんを選ばないで、残りのn-1人からr人を選ぶ => n-1Cr
let add_to_each = (num, lst) => List.map(el => [num, ...el], lst); let rec combination = (r, lst) => switch (r) { | 0 => [[]] | _ => switch (lst) { | [] => [] | [h, ...rest] => let with_h = add_to_each(h, combination(r - 1, rest)); let without_h = combination(r, rest); with_h @ without_h; } };
add_to_eachは次からmapconsに改名する