Print a List in OCaml

You should become familiar with the List.iter and List.map functions. They are essential for programming in OCaml. If you also get comfortable with the Printf module, you can then write:

open Printf
let a = [1;2;3;4;5]
let () = List.iter (printf "%d ") a

I open Printf in most of my code because I use the functions in it so often. Without that you would have to write Printf.printf in the last line. Also, if you’re working in the toploop, don’t forget to end the above statements with double semi-colons.

Leave a Comment