ocaml - 在 OCaml 中打印元组

标签 ocaml

我想打印 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但没有提到元组。

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

最佳答案

你可以去元组,例如,

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))

关于ocaml - 在 OCaml 中打印元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40288699/

相关文章:

graphics - 如何编译ocaml图形窗口cygwin

parsing - 解析的优先级

c - Nativeint Bigarray 似乎未签名

ocaml - 一个简单的 OCaml 程序

tree - OCaml 选项值树

ocaml - 使 menhir 添加用户定义的函数从 .mly 到 .mli

html - 使用 OCaml 解析 HTML

ocaml - 使用 dune 将生成的可执行文件复制到我的根目录中

ocaml - OCaml 字节码的可移植性

ocaml - 在 Num 模块中表示无穷大