【问题标题】:OCaml: codes wrapped in parenthesesOCaml:括号中的代码
【发布时间】:2012-09-12 00:26:39
【问题描述】:

我在 Developing Applications with 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;;

如果我删除“匹配”部分周围的括号,则代码将不起作用(不会打印任何内容)。知道为什么吗?

更一般地说,我什么时候应该在这样的代码位周围加上括号?

谢谢。

【问题讨论】:

    标签: printing ocaml


    【解决方案1】:

    查看它的一种方式是,在match 语句的箭头-> 之后,您可以有一系列表达式(由; 分隔)。如果没有括号,以下表达式看起来像是 match 的最后一种情况的一部分。

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

    我个人避免使用;。我就是这样处理这个问题的!否则,您必须确定表达式序列与采用序列的最内层构造相匹配。

    【讨论】:

      【解决方案2】:

      正如 Jeffrey 所解释的,如果删除括号,则 Graphics.set_color col; Graphics.draw_string str 语句将被理解为 | Left -> 案例的一部分。

      这个答案更多地是关于何时在此类代码摘录中使用括号。 在大多数情况下,模式匹配是函数的 last 表达式,例如:

      let f x y =
        foo x;
        match y with
        | Bar -> Printf.printf "Found y=Bar!\n%!"; 42
        | Baz -> Printf.printf "Found y=Baz!\n%!"; 43
      

      在这种情况下,您不需要括号。很多时候,它也是函数的第一个,因此也是唯一的表达式:

      let hd list = match list with
        | a :: _ -> a
        | [] -> invalid_arg "hd"
      

      但是当你想在匹配之后做事情时,你需要告诉OCaml匹配在哪里结束。这是您使用括号的地方:

      let f x y =
        foo x;
        (match y with
        | Bar -> 42
        | Baz -> 43);
        (* do something, both when y is Bar and when it is Baz: *)
        qux x y;
      

      这同样适用于try ... with 语句:

      let find_a_b a b list =
        (try print_int List.assoc a list
         with Not_found -> Printf.printf "Could not find a=%s.\n%!" a);
        (* Now do something whether or not we found a: *)
        (try print_int List.assoc b list
         with Not_found -> Printf.printf "Could not find b=%s.\n%!" b);
      

      这里第一个括号是强制性的,第二个是可选的,通常不写。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-27
        • 1970-01-01
        • 1970-01-01
        • 2015-12-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多