在OCaml中将字符串列表转换为字符串数组的步骤
我试图获得一个漂亮的print函数,用OCaml打印数据库的查询结果。我一直遵循这种方法 到目前为止,我有以下代码:在OCaml中将字符串列表转换为字符串数组的步骤,ocaml,Ocaml,我试图获得一个漂亮的print函数,用OCaml打印数据库的查询结果。我一直遵循这种方法 到目前为止,我有以下代码: 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
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)
;;
基本上是因为我需要元组,字符串数组是矩阵,而它的类型是字符串列表。因此,我尝试通过以下代码将列表转换为矩阵来解决此问题:
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
;;
我真的不知道该做什么,也不知道如何完成列表到矩阵的对话。我的方法正确吗?这是我第一次使用OCaml,在*,这是一个相当痛苦的过程,所以请对我好一点:D这是很多需要详细阅读的代码,但是看起来结果后面缺少了一个分号。[row].[col]这是很多需要详细阅读的代码,但是看起来结果后面缺少了一个分号。[row].[col]我不确定我是否理解了你的全部帖子,但如果你想转换 一个字符串列表变成一个字符串数组,你可以这样做 使用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"|]|]
我希望这有帮助。我不确定我是否理解了你的全部帖子,但如果你想转换 一个字符串列表变成一个字符串数组,你可以这样做 使用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"|]|]
我希望这有帮助。您的函数语法不正确。下面是一个固定版本:
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
;;
如其他答复所述:
数组的下标运算符是括号,
有些分号不见了,
有时您忘记在中使用关键字来标记将使用定义的表达式。
请注意,我没有检查函数是否执行了它应该执行的操作。您的函数语法不正确。下面是一个固定版本:
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
;;
如其他答复所述:
数组的下标运算符是括号,
有些分号不见了,
有时您忘记在中使用关键字来标记将使用定义的表达式。
请注意,我没有检查函数是否执行了它应该执行的操作
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
;;