启动OCaml开发并面临一些打嗝

时间:2013-12-14 19:14:20

标签: ocaml

我正在阅读Real World OCaml并完成一些练习。

我按照书中的说明编写了这个程序:

open Core.Std

let build_counts() = 
    In_channel.fold_lines stdin ~init:[] ~f:(fun counts line -> 
        let count = 
            match List.Assoc.find counts line with
            | None -> 0
            | Some x -> x
        in
        List.Assoc.add counts line (count + 1)
    )

let () = 
    build_counts()
    |> List.sort ~cmp:(fun (_, x) (_, y) -> Int.descending x y)
    |> (fun l -> List.take l 10)
    |> List.iter ~f: (fun (line, count) -> printf "%3d: %s\n" count line)

现在几个问题是

  1. 我跑了sudo opam install core,之后我跑了corebuild Test.ml。该命令没有抛出任何错误......但绝对没有做任何事情。它没有生成任何二进制文件。

  2. 由于第1步无效,我跑了:

    ocamlfind ocamlc -linkpkg -thread -package core Test.ml -o Test.byte

    这产生了一个我可以运行的Test.byte文件,但问题是我可以继续输入行但是我无法让程序产生任何结果,因为它处于从控制台接受输入的无限循环中。

    如果按cntl + c,则整个程序终止......再次没有评估。

  3. 如果我再次运行corebuild Test.ml,我会收到一条错误消息。

  4. 这是最后一个输出:

    SANITIZE: a total of 2 files that should probably not be in your source tree
      has been found. A script shell file
      "/home/abhishek/Documents/_build/sanitize.sh" is being created. Check this
      script and run it to remove unwanted files or use other options (such as
      defining hygiene exceptions or using the -no-hygiene option).
    IMPORTANT: I cannot work with leftover compiled files.
    ERROR: Leftover OCaml compilation files:
      File Test.cmo in . has suffix .cmo
      File Test.cmi in . has suffix .cmi
    Exiting due to hygiene violations.
    

    所以事情并没有像书中所说的那样。我对OCaml工具和语言有困难。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:3)

我无法解决第一个问题。

对于第二个问题,假设您正在使用类似Unix的系统:输入^ D(控制 D)结束你的输入。您还可以从文件创建文件并重定向:

$ Test.byte < my-input-file

请注意,这不是OCaml语言问题。它是关于处理文件的 您的系统(或可能与命令行环境有关)。

对于第三个问题,请删除.cmo和.cmi文件,然后重试,如消息所示。

答案 1 :(得分:3)

关于第一个问题,ocamlbuild(corebuild是一个包装器)通过指定你想要的结果来工作。

你可能有一个
corebuild test.byte
corebuild test.native
corebuild test.cmo

.byte.native将指定字节编译和本机编译的可执行文件,而.cmo将指定只编译文件(如果_build下面将{{1}}编译成功)。

答案 2 :(得分:0)

我的回答可能有点晚,但这对我有用。我得到了你粘贴的相同输出,我运行文件/home/abhishek/Documents/_build/sanitize.sh删除所有“不需要的文件”然后我​​执行了corebuild freq.byte,它运行良好。在你的情况下,你应该运行corebuild test.byte