【问题标题】:Print tuple in OCaml在 OCaml 中打印元组
【发布时间】:2017-03-10 09:06:13
【问题描述】:

我想打印 int * int 类型的方法的结果。

我试过这样:

printf "%d %d\n" (find (99, 10));;

但是,我得到:

Error: This expression has type int * int
       but an expression was expected of type int

我在这里查看http://caml.inria.fr/pub/docs/manual-ocaml/libref/Printf.html,但没有提到元组。

那么打印元组的正确方法是什么?

【问题讨论】:

    标签: ocaml


    【解决方案1】:

    你可以去元组,例如,

    let (x,y) = find (99,10) in 
    printf "%d %d\n" x y
    

    或者,您可以定义一个元组打印机,例如,

    let pp_int_pair ppf (x,y) =
      fprintf ppf "(%d,%d)" x y
    

    然后将其与%a 说明符一起使用,例如,

    printf "%a\n" pp_int_pair (find (99,10))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-05
      • 2018-09-19
      相关资源
      最近更新 更多