3
votes

I want to print the result of a method which is of type int * int.

I tried like this:

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

However, I'm getting:

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

I looked here http://caml.inria.fr/pub/docs/manual-ocaml/libref/Printf.html but there is no mention of tuple.

So what's the right way to print a tuple?

1

1 Answers

9
votes

You can detuple it, e.g.,

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

Alternatively, you can define a tuple printer, e.g.,

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

and then use it with the %a specifier, e.g.,

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