Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm very late answering, but here's another way:</p> <pre><code>let print_list f lst = let rec print_elements = function | [] -&gt; () | h::t -&gt; f h; print_string ";"; print_elements t in print_string "["; print_elements lst; print_string "]";; </code></pre> <p>To print an int list, we could write:</p> <pre><code>print_list print_int [3;6;78;5;2;34;7];; </code></pre> <p>However if we were going to do this a lot, it would save time to specialize the function using partial application:</p> <pre><code>let print_int_list = print_list print_int;; </code></pre> <p>Which we can now use like so:</p> <pre><code>print_int_list [3;6;78;5;2;34;7];; </code></pre> <p>What if we wanted to do something pretty complex, like printing an int list list? With this function, it's easy:</p> <pre><code>(* 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]];; </code></pre> <p>Printing an (int * string) list (i.e. a list of pairs of ints and strings):</p> <pre><code>(* Option 1 *) print_list (fun (a, b) -&gt; 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")];; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload