Print a list of tuples

Hey, I am looking for a way to print structures for debugging purposes, and for other purposes. For example, I have a function that produces a list of tuples:

 List.mapi (
        fun i (name, address) -> 
          Printf.sprintf "Level: %d, name: %d, address: %d" i name address
      ) l
      |> String.concat " " 

Is there a simpler way to print it, maybe using [@@deriving sexp] I found in Base?

If you don’t mind using PPX, you can use the [@@deriving show] plugin, see GitHub - ocaml-ppx/ppx_deriving: Type-driven code generation for OCaml.

Cheers,
Nicolas

3 Likes

You can also use Fmt combinators, something like

let pp_tupl = Fmt.Dump.(list (pair Fmt.int Fmt.int)) in
Format.asprintf "%a" pp_tupl my_value
3 Likes