OCaml:括在括号中的代码

xys*_*sun 2 printing ocaml

我在使用OCaml开发应用程序时遵循此draw_string_in_box示例

给出的示例代码如下:

let draw_string_in_box pos str bcf col = 
 let (w, h) = Graphics.text_size str in
 let ty = bcf.y + (bcf.h-h)/2 in 
 ( match pos with 
       Center -> Graphics.moveto (bcf.x + (bcf.w-w)/2) ty 
     | Right  -> let tx = bcf.x + bcf.w - w - bcf.bw - 1 in 
                 Graphics.moveto tx ty 
     | Left   -> let tx = bcf.x + bcf.bw + 1 in Graphics.moveto tx ty  );
 Graphics.set_color col;
 Graphics.draw_string str;;
Run Code Online (Sandbox Code Playgroud)

如果我删除"匹配"部分周围的括号,代码将无法工作(没有任何内容被打印).知道为什么吗?

更一般地说,我应该何时将括号括在这样的代码位?

谢谢.

Jef*_*eld 5

看它的方法之一是,该箭头后->一的match声明中,你可以有表达的序列(分立;).没有括号,下面的表达式看起来像是最后一个案例的一部分match.

您也可以在之后有一系列表达式(以分隔开;)let.使用括号,以下表达式看起来像是它们的一部分let,这就是您想要的.

我个人避免使用;.这就是我如何处理这个问题!否则,您必须确定表达式序列与采用序列的最内层构造一致.