检查Ocaml StringMap中是否存在word
我是OCaml新手,正在尝试从单词列表中创建单词计数。对于每一个单词,我都在尝试这样做:检查Ocaml StringMap中是否存在word,ocaml,word-count,Ocaml,Word Count,我是OCaml新手,正在尝试从单词列表中创建单词计数。对于每一个单词,我都在尝试这样做: let check x = if StringMap.mem x then y = StringMap.find x testMap (* I want to add one to this value *) else let testMap = StringMap.add x 1 testMap ;; 除了在这段代码中出现错误外,我很确定我的逻辑也有点错误。我不熟悉函数式编程,所以任何帮助都会很好。
let check x = if StringMap.mem x then y = StringMap.find x testMap (* I want to add one to this value *)
else
let testMap = StringMap.add x 1 testMap ;;
除了在这段代码中出现错误外,我很确定我的逻辑也有点错误。我不熟悉函数式编程,所以任何帮助都会很好。
StringMap.add
是一个新的map;原来的地图没有动过
要取得进展,您需要跟踪地图的当前值
检查的一种可能性是
let check : string -> StringMap.t -> StringMap.t
= fun string map ->
if StringMap.mem string map
then let y = StringMap.find string map in
(* do something with y *)
else (* add a binding between string and 1 *)
您需要在此处完成两个注释部分