在OCaml中打印元组

时间:2016-10-27 15:32:17

标签: 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,但没有提到元组。

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

1 个答案:

答案 0 :(得分:8)

你可以贬低它,例如,

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))
相关问题