我想打印 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/