2017-06-18 54 views
0

我要比較兩個哈希表的平等:OCaml的 - 兩個哈希表的平等和函數嵌套標記參數

open Core.Std 

let hashtables_equal (x_tbl: ('a, 'b) Hashtbl.t) (y_tbl: ('a, 'b) Hashtbl.t) : bool = 
    (Hashtbl.length x_tbl = Hashtbl.length y_tbl) 
    && Hashtbl.for_alli x_tbl ~f:(fun ~key ~data -> Hashtbl.existsi y_tbl ~f:(fun ~k ~d -> k = key && d = data)) 

for_alli功能fexistsi有兩個標記參數~key~data

上面的代碼由於使用了不正確的標籤而無法編譯。但是,我想在嵌套函數中引用~key~data標記的參數。

我該怎麼做?

回答

0

您可以給一個帶標籤參數的本地名稱,您不需要使用標籤本身作爲名稱。

let hashtables_equal x_tbl y_tbl = 
    Hashtbl.length x_tbl = Hashtbl.length y_tbl && 
    Hashtbl.for_alli x_tbl ~f: (fun ~key ~data -> 
     Hashtbl.existsi y_tbl ~f:(fun ~key:k ~data:d -> 
      k = key && d = data)) 

對於它的價值,似乎是在覈心功能,對平等的比較哈希表:在這種情況下

let hashtables_equal x_tbl y_tbl = Hashtbl.equal x_tbl y_tbl (=) 

最後一個參數,(=),是在情況比較元素通常的比較不是你想要的。 (因爲它不會是散列表,作爲一個例子。)