Print a List in OCaml

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

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?

回答1:

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.



回答2:

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.



回答3:

print_string (String.concat " " (List.map string_of_int list)) 


回答4:

If the question is about finding the quickiest way to implement this, for example when debugging, then we could say that:

  • extended standard libraries (e.g. batteries) typically have some additional functions:

    List.print   ~first:"[" ~sep:";" ~last:"]" (fun c x -> Printf.fprintf c "%d" x) stdout a 
  • this tiny syntax extension that I wrote some time ago allows you to write:

    <:print a="">> 
  • automatic generation is not immediately available (because of the lack of run-time type information in OCaml data representation) but can be achieved using either code generation from the types, or run-time types.


回答5:

I'm very late answering, but here's another way:

let print_list f lst =   let rec print_elements = function     | [] -> ()     | h::t -> f h; print_string ";"; print_elements t   in   print_string "[";   print_elements lst;   print_string "]";; 

To print an int list, we could write:

print_list print_int [3;6;78;5;2;34;7];; 

However if we were going to do this a lot, it would save time to specialize the function using partial application:

let print_int_list = print_list print_int;; 

Which we can now use like so:

print_int_list [3;6;78;5;2;34;7];; 

What if we wanted to do something pretty complex, like printing an int list list? With this function, it's easy:

(* Option 1 *) print_list (print_list print_int) [[3;6;78];[];[5];[2;34;7]];;  (* Option 2 *) let print_int_list_list = print_list (print_list print_int);; print_int_list_list [[3;6;78];[];[5];[2;34;7]];;  (* Option 3 *) let print_int_list_list = print_list print_int_list;; print_int_list_list [[3;6;78];[];[5];[2;34;7]];; 

Printing an (int * string) list (i.e. a list of pairs of ints and strings):

(* Option 1 *) print_list (fun (a, b) -> print_string "("; print_int a; print_string ", "; print_string b; print_string ")") [(1, "one"); (2, "two"); (3, "three")];;  (* Option 2 *) let print_pair f g (a, b) =   print_string "(";   f a;   print_string ", ";   g b;   print_string ")";; print_list (print_pair print_int print_string) [(1, "one"); (2, "two"); (3, "three")];;  (* Option 3 *) let print_pair f g (a, b) =   print_string "(";   f a;   print_string ", ";   g b;   print_string ")";; let print_int_string_pair = print_pair print_int print_string;; print_list print_int_string_pair [(1, "one"); (2, "two"); (3, "three")];;  (* Option 4 *) let print_pair f g (a, b) =   print_string "(";   f a;   print_string ", ";   g b;   print_string ")";; let print_int_string_pair = print_pair print_int print_string;; let print_int_string_pair_list = print_list print_int_string_pair;; print_int_string_pair_list [(1, "one"); (2, "two"); (3, "three")];; 


回答6:

I would do this in the following way:

let a = [1;2;3;4;5];; List.iter print_int a;; 


回答7:

let print_list l =   let rec aux acc =     match acc with      | [] -> ()      | x :: tl ->        Printf.fprintf stdout "%i"; aux tl    in aux l 

Or

let sprintf_list l =   let acc = ref "{" in   List.iteri (fun i x ->     acc := !acc ^       if i  0       then Printf.sprintf "; %i" x       else Printf.sprintf "%i" x   ) l;   !acc ^ "}"  let print_list l =   let output = sprintf_list l in   Printf.fprintf stdout "%s\n" output 


回答8:

Just a solution with %a :

open Printf let print_l outx l =       List.map string_of_int  l   |> String.concat ";"   |> fprintf outx "%s" 

Test :

# printf "[%a]" print_l [1;2;3] ;; [1;2;3]- : unit = () # printf "[%a]" print_l [];; []- : unit = () 


文章来源: Print a List in OCaml
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!