user1906589
user1906589

Reputation: 53

To convert a a string list list to an string array array in OCaml

I'm trying to get a pretty print function to print the query result of my database in OCaml. I've been following this approach http://mancoosi.org/~abate/ocaml-format-module

I have this code so far:

let pp_cell fmt cell = Format.fprintf fmt "%s" cell;;

let pp_header widths fmt header =
  let first_row = Array.map (fun x -> String.make (x + 1) ' ') widths in
  Array.iteri (fun j cell ->
    Format.pp_set_tab fmt ();
    for z=0 to (String.length header.(j)) - 1 do cell.[z] <- header.(j).[z] done;
    Format.fprintf fmt "%s" cell
  ) first_row

let pp_row pp_cell fmt row =
  Array.iteri (fun j cell ->
    Format.pp_print_tab fmt ();
    Format.fprintf fmt "%a" pp_cell cell
  ) row


let pp_tables pp_row fmt (header,table) =
  (* we build with the largest length of each column of the 
   * table and header *)
  let widths = Array.create (Array.length table.(0)) 0 in
  Array.iter (fun row ->
    Array.iteri (fun j cell ->
      widths.(j) <- max (String.length cell) widths.(j)
    ) row
  ) table;
  Array.iteri (fun j cell ->
    widths.(j) <- max (String.length cell) widths.(j)
  ) header;

  (* open the table box *)
  Format.pp_open_tbox fmt ();

  (* print the header *)
  Format.fprintf fmt "%a@\n" (pp_header widths) header;
  (* print the table *)
  Array.iter (pp_row fmt) table;

  (* close the box *)
  Format.pp_close_tbox fmt ();
;;


(** Pretty print answer set of a query in format of
 *    col_name 1 | col_name 2 | col_name 3 |
 *    result1.1  | result2.1  | result3.1  |
 *    result1.2  | result2.2  | result3.2  |
 *   @param col_names provides the names of columns in result outp ut *)
let pretty_print fmt pp_cell (col_names, tuples) =
    match col_names with
  | [] -> printf "Empty query\n"
  | _ ->
        printf "Tuples ok\n";
        printf "%i tuples with %i fields\n" (List.length tuples) (List.length col_names);
        print_endline(String.concat "\t|" col_names);
        for i = 1 to List.length col_names do printf "--------" done; print_newline() ;
        let print_row = List.iter (printf "%s\t|") in
        List.iter (fun r -> print_row r ; print_newline ()) tuples;
        for i = 1 to List.length col_names do printf "--------" done; print_newline() ;

    let fmt = Format.std_formatter in
    Format.fprintf fmt "%a" (pp_tables (pp_row pp_cell)) (Array.of_list col_names,tuples);

      flush stdout
;;

let print_res (col_names, tuples) =
    let fmt = Format.std_formatter in
    pretty_print fmt pp_cell (col_names, tuples)
;;

The problem is in the line

Format.fprintf fmt "%a" (pp_tables (pp_row pp_cell)) (Array.of_list col_names,tuples);

basically because I need tuples to be and string array array (a matrix) while its type is string list list. So I tried to solve it by converting the list list into a matrix following this approach http://www.siteduzero.com/forum-83-589601-p1-ocaml-convertir-un-list-list-en-array-array.html with this code:

let listToMatrix lli =
  let result = Array.init 6 (fun _ -> Array.create 7 2)
  let rec outer = function
    | h :: tl, col ->
      let rec inner = function
        | h :: tl, row ->
          result.[row].[col] <- h
          inner (tl, row + 1)
        | _ -> ()

      inner (h, 6 - List.length h)
      outer (tl, col + 1)
    | _ -> ()
  outer (lli, 0)
  result
;;

But I just a syntax error while compiling:

File "src/conn_ops.ml", line 137, characters 2-5:
Error: Syntax error
make: *** [bin/conn_ops.cmo] Error 2

I don't really know what to do, or how I can accomplish the conversation of the list list into the matrix. My approach is the correct? This has been the first time I've worked with OCaml and it's been quite a pain in the *, so please, try to be kind with me :D

Upvotes: 1

Views: 4998

Answers (3)

didierc
didierc

Reputation: 14730

Your function is not syntacticly correct. Below is a fixed version:

let listToMatrix lli =
  let result = Array.init 6 (fun _ -> Array.create 7 2) in
  let rec outer = function
    | h :: tl, col ->
      let rec inner = function
    | h :: tl, row ->
      result.(row).(col) <- h;
      inner (tl, row + 1)
    | _ -> ()
      in
      inner (h, 6 - List.length h);
      outer (tl, col + 1)
    | _ -> ()
  in 
  outer (lli, 0);
  result
;;

As noted in other answers:

  • the subscript operators for arrays are parentheses,
  • some semi-colons are missing,
  • sometimes you forget to use the keyword in to mark the expression where your definition will be used.

Please note that I didn't check if the function does what it is supposed to do.

Upvotes: 0

gnuvince
gnuvince

Reputation: 2387

I'm not sure I understood your entire post, but if you want to convert a string list list into a string array array, you can do this quite easily with the Array.of_list function:

# let strings = [["hello"; "world"]; ["foo"; "bar"]];;
val strings : string list list = [["hello"; "world"]; ["foo"; "bar"]]
# Array.of_list (List.map Array.of_list strings);;
- : string array array = [|[|"hello"; "world"|]; [|"foo"; "bar"|]|]

I hope this helped.

Upvotes: 3

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66818

This is a lot of code to read in detail, but it looks like you're missing a semicolon after result.[row].[col] <- h. However, this code looks suspicious to me. The notation .[xxx] is for accessing individual characters of a string. You want to use array index notation .(xxx), seems to me.

Here is a function that changes a string list list to a string array array. Maybe it will be useful:

let sll_to_saa sll = Array.of_list (List.map Array.of_list sll)

Actually, this function changes any list of lists to an array of arrays; it doesn't have to be strings.

Upvotes: 4

Related Questions