Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine type of 2 corresponding sets

I have declared a variable al : 'a list, a function a_to_b : 'a -> 'b and a function score : 'b -> int. Then let bl = List.map a_to_b al in ... in the following code defines bl : 'b list.

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 

This piece of code finds a b_best such that its score is greatest. But one of my needs is that I also want to know, which a_best generates this b_best via a_to_b, and there is no way. For instance, if b_best is the 4th element in the bl, I consider the 4th elment of al is what I want to get.

I don't want to add more parameters to the function find_best. My question is if there is a conventional way to define the type of al and bl, to make it easy to trace a_best from b_best, for instance, using array instead of list? or convert to array then convert to list back?

like image 758
SoftTimur Avatar asked Dec 20 '25 05:12

SoftTimur


1 Answers

You can do something like that:

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 *)
like image 100
Çağdaş Bozman Avatar answered Dec 21 '25 20:12

Çağdaş Bozman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!