List 如何在ocaml中将函数参数和返回类型指定为列表?

List 如何在ocaml中将函数参数和返回类型指定为列表?,list,ocaml,List,Ocaml,我目前正在为一门编程语言课程自学ocaml,我试图弄清楚如何将函数参数和返回类型指定为列表 我创建了一个程序,通过char读取文件char,将每个char存储在列表中,反转列表,然后返回列表 当前代码: (* Creating a function that will read all the chars in a file passed in from the command argument. This function takes a parameter of typ

我目前正在为一门编程语言课程自学
ocaml
,我试图弄清楚如何将函数参数和返回类型指定为
列表

我创建了一个程序,通过
char
读取文件
char
,将每个
char
存储在
列表中,反转列表,然后返回
列表

当前代码:

(* 
   Creating a function that will read all the chars 
   in a file passed in from the command argument.
   This function takes a parameter of type List.
   This function will return a List. 
*)

let read_file (char_List : List) : List =
    let char_in = open_in Sys.argv.(1) in   (* Creating a file point/in_channel *)
  try
    while true do
      let c = input_char char_in in     (* Getting char from the file *)
        char_List := c :: !char_List    (* Storing the char in the list *)
    done
  with End_of_file ->
        char_List := List.rev !char_List;   (* End of file was reaching reversing char list *)
        close_in char_in;                   (* Closing the file pointer/in_channel *)
;;

(* Storing the result of read_file to buffer which buffer is of type list *)
let buffer = ref [] in
      read_file(buffer);

      print_string "\nThe length of the buffer is: ";
      print_int (List.length !buffer); (* Printing length of the list *)
      print_string ("\n\n");
      List.iter print_char !buffer;    (* Iterating through the list and print each element *)
如果我取消指定
List
的参数类型和返回类型,代码将按预期运行。然而;我想将参数的类型和返回类型指定为
列表


如何将函数参数和返回类型指定为
列表
。但是,您不能仅使用
列表
进行注释,因为列表本身不是一种类型:您不希望有一个不可知事物的列表,而是一个自身具有已知类型的元素列表。例如,在您的例子中,您有一个字符列表,可以写成
charlist
。类似地,整数列表将被键入
int list

更准确地说,
list
本身不是一个类型,而是一个类型构造函数,它将列表元素的类型作为参数,并返回此类元素列表的类型


p、 美国:如果您正在学习OCaml,您可以尝试重写代码,而无需使用引用来适应更具功能性的样式。

首先,
List
是一个模块,而不是一种类型,因此您可能指的是
List
。但是,您不能仅使用
列表
进行注释,因为列表本身不是一种类型:您不希望有一个不可知事物的列表,而是一个自身具有已知类型的元素列表。例如,在您的例子中,您有一个字符列表,可以写成
charlist
。类似地,整数列表将被键入
int list

更准确地说,
list
本身不是一个类型,而是一个类型构造函数,它将列表元素的类型作为参数,并返回此类元素列表的类型


p、 美国:如果您正在学习OCaml,您可以尝试重写代码,而无需使用引用来适应更具功能性的样式。

正如@octachron正确指出的那样,
List
在OCaml中不是正确的类型。你可能是指一个列表。查看您的代码,您可以通过解决以下两点来更正代码:

  • 按如下所示更正函数签名,
    让我们读取文件(char\u List:'char List ref:'char List=
  • 添加
    !完成和
    后的字符列表
    !字符列表
您更正的代码可能如下所示:

let read_file (char_List: 'char list ref) : 'char list =
    let char_in = open_in Sys.argv.(1) in   (* Creating a file point/in_channel *)
  try
    while true do
      let c = input_char char_in in     (* Getting char from the file *)
      char_List := c :: !char_List    (* Storing the char in the list *)
    done;
    !char_List
  with End_of_file ->
    char_List := List.rev !char_List;   (* End of file was reaching reversing char list *)
    close_in char_in;                   (* Closing the file pointer/in_channel *)
    !char_List
然而,虽然这是可行的,但您可能希望在ocaml中使用功能更强大的方法。一个没有变异且具有递归功能的版本可以实现如下:

let get_chars file =
  let rec loop ic acc =
    match Pervasives.input_char ic with
    | c -> loop ic (c::acc)
    | exception(End_of_file) -> List.rev acc
  in
  let ic = Pervasives.open_in file in
  loop ic []
然后,在ocaml toploop(repl)中,您可以这样执行函数

  • get_chars”/tmp/union_find.ml
或许

  • 获取字符系统argv.(1)

正如@octachron正确指出的那样,
列表在ocaml中不是正确的类型。你可能是指一个列表。查看您的代码,您可以通过解决以下两点来更正代码:

  • 按如下所示更正函数签名,
    让我们读取文件(char\u List:'char List ref:'char List=
  • 添加
    !完成和
    后的字符列表
    !字符列表
您更正的代码可能如下所示:

let read_file (char_List: 'char list ref) : 'char list =
    let char_in = open_in Sys.argv.(1) in   (* Creating a file point/in_channel *)
  try
    while true do
      let c = input_char char_in in     (* Getting char from the file *)
      char_List := c :: !char_List    (* Storing the char in the list *)
    done;
    !char_List
  with End_of_file ->
    char_List := List.rev !char_List;   (* End of file was reaching reversing char list *)
    close_in char_in;                   (* Closing the file pointer/in_channel *)
    !char_List
然而,虽然这是可行的,但您可能希望在ocaml中使用功能更强大的方法。一个没有变异且具有递归功能的版本可以实现如下:

let get_chars file =
  let rec loop ic acc =
    match Pervasives.input_char ic with
    | c -> loop ic (c::acc)
    | exception(End_of_file) -> List.rev acc
  in
  let ic = Pervasives.open_in file in
  loop ic []
然后,在ocaml toploop(repl)中,您可以这样执行函数

  • get_chars”/tmp/union_find.ml
或许

  • 获取字符系统argv.(1)

为什么写pervisives.input_char/open_in而不是直接input_char/open_in?@ghilesZ主要是为了明确函数/val的来源。为什么写pervisives.input_char/open_in而不是直接input_char/open_in?@ghilesZ主要是为了明确函数/val的来源。