【发布时间】:2014-11-12 03:55:07
【问题描述】:
我开始在 OCaml 中编写此代码,以获取文件中每个字符的计数。
open Hashtbl;;
type 'a option = None | Some of 'a;;
let rec charCount fd ht =
let x =
try Some (input_char fd)
with End_of_file -> None
in
match x with
| Some c ->
let val =
try find (ht c)
with Not_found -> 0
in
replace ht c (val+1);
charCount fd ht
| None -> ();;
let ht = create 0;;
let loadHisto fn =
let fd = open_in fn
in
charCount fd ht;;
loadHisto "testfile";;
iter printf("%s => %s\n") ht;;
当我尝试使用 ocamlc -c 编译它时,我收到了消息:
Error: Syntax error:
【问题讨论】:
-
作为旁注,我建议不要在实际代码中打开
Hashtbl模块。如果这是它的预期用途,那么它的函数就需要使用更少的通用名称。
标签: syntax-error ocaml