将字符串列表列表转换为OCaml中的字符串数组数组

时间:2012-12-15 17:13:56

标签: ocaml

我正在尝试使用漂亮的打印功能在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

3 个答案:

答案 0 :(得分:4)

这是很多详细阅读的代码,但看起来你在result.[row].[col] <- h之后错过了一个分号。但是,这段代码看起来很可疑。符号.[xxx]用于访问字符串的各个字符。在我看来,你想使用数组索引表示法.(xxx)

以下是将string list list更改为string array array的功能。也许它会有用:

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

实际上,此函数会将任何列表列表更改为数组数组;它不一定是字符串。

答案 1 :(得分:2)

我不确定我理解你的整个帖子,但是如果你想转换 将string list list改为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"|]|]

我希望这有帮助。

答案 2 :(得分: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来标记将使用您的定义的表达式。

请注意,我没有检查该功能是否符合预期。