https://ocaml.org/learn/tutorials/99problems.htmlocaml的99個問題:無法理解的溶液,用於產生組合
我想了解,用於從一個列表的N個元素選擇的k個不同的對象的組合的溶液中。下面是代碼:
let extract k list =
let rec aux k acc emit = function
| [] -> acc
| h :: t ->
if k = 1 then aux k (emit [h] acc) emit t else
let new_emit x = emit (h :: x) in
aux k (aux (k-1) acc new_emit t) emit t
in
let emit x acc = x :: acc in
aux k [] emit list;;
的發射功能被定義爲接受兩個參數:
let emit x acc = x :: acc
所以我不太明白下面一行是如何工作的,因爲它發出的呼叫只給一個參數:
let new_emit x = emit (h :: x)
此外,new_emit函數只接受一個參數,並作爲參數傳遞給AUX功能傳遞,怎麼能應對以下行(這裏是EMIT通過提供兩個參數)呼籲:OCaml中
if k = 1 then aux k (emit [h] acc) emit t
的回答言簡意賅感謝 – ccy1997