Ocaml字符串直方图

时间:2016-12-14 08:51:47

标签: string ocaml histogram

我正在尝试从OCaml中的字符串做直方图。 我处于这种状态,编辑一点:create a histogram OCaml以使用字符串输入。

如何查看charFreqs的内容,或者它是否有效?

module S = String;;
module HT = Hashtbl;;

let histogram s =
    let charFreqs = HT.create 126 in
        S.iter (fun c ->
            let tmp =
                try HT.find charFreqs c
                with Not_found -> 0 in
            HT.replace charFreqs c (tmp+1)
        ) s;;

let str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";;
let charFreqs = histogram str;;
  

输出:val charFreqs:unit =()

1 个答案:

答案 0 :(得分:2)

您的histogram函数返回S.iter的结果,即unit

val iter : (char -> unit) -> string -> unit

尝试类似:

let histogram s =
    let charFreqs = HT.create 126 in
        S.iter (fun c ->
            let tmp =
                try HT.find charFreqs c
                with Not_found -> 0 in
            HT.replace charFreqs c (tmp+1)
        ) s;
        charFreqs;;