OCaml学习——《Real-World-Ocaml》Chap4
Chap4 文件、模块和程序
(本章很多内容 15 年中文版不适用,需要 2nd Edition 英文版,现在已经转向英文版学习)
从练习转向实践,抛开顶层环境,由文件来构建程序。
文件不只是存储和管理代码的一种方便方式,在 OCaml 中,文件还与模块对应,相当于边界,可以把程序划分为不同的概念单元。
单文件程序
实际例子,从stdin
读入输入行,并计算各行的频数,最后,会写出频数最高的行。构建freq.ml
:
1 |
|
funciton build_counts
readds in lines from stdin
, constructing from those lines an association list with the frequencies of each line.
Main 函数?
OCaml 与 C 不同,程序并没有一个唯一的
main
函数。执行程序时,实现文件中的所有语句会按其链接的顺序进行计算。某种程度上,整个代码基都是一个庞大的 main 函数。写
let () =
是一个惯用法,这里 let 绑定是对一个 unit 类型值的模式匹配,它是为了确保右边的表达式返回 unit,对于主要为得到副作用的函数来说,这个用法很常见。
直接使用ocamlopt freq.ml freq
编译该文件会出现编译错误,因为无法找到Base
和Stdio
。使用ocamlfind
找寻依赖或者使用dune
。
使用ocamlfind
找到对应的 pkg 再进行编译。
1 |
|
BYTECODE VERSUS NATIVE CODE
OCaml ships with two compilers
ocamlopt
: the native code compilerocamlc
: the bytecode compiler
Aside from performance, executables generated by the two compilers have nearly identical behavior. There are a few things to be aware of.
- The bytecode compiler can be used on more architectures, and has some tools that are not available for native code. (although gdb, the GNU Debugger, works with some limitations on OCaml native-code applications).
- The bytecode compiler is also quicker than the native-code compiler.
- In order to run a bytecode executable, you typically need to have OCaml installed on the system in question. That's not strictly required, though, since you can build a bytecode executable with an embedded runtime, using the
-custom
compiler flag.
多文件程序和模块
Souce files in Ocaml are tied into the module system, with each file compiling down into a module whose name is derived from the name of the file. At its simplest, you can think of a module as a collection of definitions that are stored within a namespace.
每个文件编译为一个模块。将之前的freq.ml
拆分进行练习。拆分出counter.ml
,会编译为名为Counter
的模块。
After refactor
1 |
|
1 |
|
1 |
|
Hint: counter.ml
should be placed before freq.ml
as the later relied on the former.
签名和抽象类型
签名(signature),在.mli
中。例如filename.ml
定义的模块会受文件filename.mli
中签名的约束。
语法
1 |
|
可以要求 OCaml 从源文件自动生成一个,然后修改这个生成文件来满足需求。
1 |
|
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!