1

我正在尝试获得一个漂亮的打印功能来在 OCaml 中打印我的数据库的查询结果。我一直在遵循这种方法http://mancoosi.org/~abate/ocaml-format-module

到目前为止我有这个代码:

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)
;;

问题出在一线

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

基本上是因为我需要元组和字符串数组数组(矩阵),而它的类型是字符串列表。所以我试图通过按照这种方法将列表列表转换为矩阵来解决它http://www.siteduzero.com/forum-83-589601-p1-ocaml-convertir-un-list-list-en-array-array .html与此代码:

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
;;

但我只是在编译时出现语法错误:

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

我真的不知道该怎么做,也不知道如何完成列表列表到矩阵的对话。我的做法正确吗?这是我第一次与 OCaml 合作,这对*来说非常痛苦,所以请试着对我好一点:D

4

3 回答 3

4

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.

于 2012-12-15T17:23:42.863 回答
3

我不确定我是否理解您的整个帖子,但如果您想将 a 转换string list list为 a string array array,您可以使用以下函数轻松完成此操作Array.of_list

# 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"|]|]

我希望这会有所帮助。

于 2012-12-15T17:28:44.267 回答
0

您的函数在语法上不正确。以下是固定版本:

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
;;

如其他答案所述:

  • 数组的下标运算符是括号,
  • 缺少一些分号,
  • 有时您忘记使用关键字in来标记将使用您的定义的表达式。

请注意,我没有检查该函数是否完成了它应该做的事情。

于 2012-12-15T19:10:08.427 回答