Search code examples
ocaml

Print a List in OCaml


I want to do something as simple as this:

Print a list.

let a = [1;2;3;4;5]

How can I print this list to Standard Output?


Solution

  • You can do this with a simple recursion :

    let rec print_list = function 
    [] -> ()
    | e::l -> print_int e ; print_string " " ; print_list l
    

    The head of the list is printed, then you do a recursive call on the tail of the list.