변수 al : 'a list
, 함수 a_to_b : 'a -> 'b
및 함수 score : 'b -> int
을 선언했습니다. 다음 코드에서 let bl = List.map a_to_b al in ...
은 bl : 'b list
을 정의합니다.대응하는 2 세트의 유형을 결정하십시오
let find_best (bl : 'b list) : 'b =
let score_best, b_best = List.fold_left
(fun (score_old, b_old) b_new ->
let score_new = score b_new in
if score_old < score_new then
(score_new, b_new) else
(score_old, b_old))
(score (List.hd bl), List.hd bl) bl in
b_best
let bl = List.map a_to_b al in
find_best bl
이 코드 조각은 score
가 큰하다는 b_best
등을 찾습니다. 하지만 내 요구 사항 중 하나는 a_best
이 b_best
을 통해 a_to_b
을 생성하고 어떤 방식 으로든 알고 싶어한다는 것입니다. 예를 들어, b_best
이 bl
의 네 번째 요소 인 경우, 제 4 번째 엘름을 al
으로 생각합니다.
더 많은 매개 변수를 함수 find_best
에 추가하고 싶지 않습니다. 내 질문은 대신 array
을 사용하여 a_best
을 b_best
에서 쉽게 추적 할 수 있도록 al
및 bl
유형을 정의하는 일반적인 방법이 있습니까? 또는에 array
변환 후 list
로 변환? 난 그냥 쌍의 목록을 가지고 한 쌍을 반환 b_best
을 정의하는 것
let abl = List.combine bl al in (* ('b * 'a) list *)
let a_best = List.assoc b_best abl (* returns the value associated to b_best *)
그런데'find_best' 함수의 정확한 이름은'argmax'입니다 : http://en.wikipedia.org/wiki/Arg_max –