如何使用OCaml Containers.CCIO更改文本

如何使用OCaml Containers.CCIO更改文本,ocaml,Ocaml,在ocaml容器文档中,它给出了一个很好的示例,即如何读入文件并将内容写入不同的文件。但是,我试图了解在将文本从正在读取的文件传递到正在写入的文件之前,修改文本需要什么 let read_filename = "example.ts" let filename = "example2.ts" let () = let modify_file ~chunks = let r = Str.regexp "text \\([A-Za-z]+\\)" in Str.replac

ocaml容器
文档中,它给出了一个很好的示例,即如何读入文件并将内容写入不同的文件。但是,我试图了解在将文本从正在读取的文件传递到正在写入的文件之前,修改文本需要什么

let read_filename = "example.ts"
let filename = "example2.ts"

let () =
  let modify_file ~chunks = 
    let r =  Str.regexp "text \\([A-Za-z]+\\)" in
    Str.replace_first r "\\1" chunks in

  CCIO.(
    with_in read_filename
      (fun ic ->
         let chunks = read_chunks ic in
         let new_chunks = modify_file chunks in
         with_out ~flags:[Open_binary] ~mode:0o644 filename
           (fun oc ->
              write_gen oc new_chunks
           )
      )
  )
此代码的问题在于编译器抱怨:

File "component.ml", line 13, characters 39-45:
Error: This expression has type string gen = unit -> string option
       but an expression was expected of type string

我试图找出我做错了什么,但没有用。任何帮助都将不胜感激。此外,对于在OCaml中使用哪种理想的企业软件来修改文件中的文本,我们非常感激。多谢各位

修改文件的类型是什么。我在手册上没有看到。 您应该尝试以这种方式添加单位值
()

let new_chunks=在
中修改_文件块(),
new_chunks
将是字符串选项类型 您可以将模式匹配作为选项类型

编辑:

我已经看到第三个参数的类型是
stringgen
您应该这样修改第16行:
write\u gen oc(fun()->新块)

您有打字问题
read\u chunk ic
返回
字符串gen

从中我们了解到,这是一个函数,它接受单位值
()
,并返回字符串

let () =
  CCIO.(
    let modify_file ~chunks = 
      let r =  Str.regexp "example \\([A-Za-z]+\\)" in
      match chunks () with
        None -> chunks (* is the same as (fun () -> None) *)
      | Some chunks ->
        let chunks = Str.replace_first r "\\1" chunks in (* compute once *)
        (fun () -> Some chunks) in
    with_in read_filename
      (fun ic ->
         let chunks = read_chunks ic in
         let new_chunks = modify_file ~chunks in
         with_out ~flags:[Open_binary] ~mode:0o644 filename
           (fun oc ->
              write_gen oc new_chunks
           )
      )
  )
编辑: 解释错误和更改

编辑2:
我已修改了modifier modify_文件,使其返回另一个字符串gen,并更正了语法错误:当您使用带标签的参数时,您需要在调用它时添加一个波浪号

我的歉意,无法100%确定我是否遵循。请您将修改后的代码全部提交。即上述问题中的代码以及您建议的修改?谢谢。^对于上述内容,我仍然收到错误:``File“component.ml”,第13行,字符38-44:错误:此表达式具有类型string gen=unit->string选项,但首先需要类型string``的表达式,谢谢!编译器将不再抱怨。但是,如果文件变得无限大,并且可执行文件从未完成,则运行此可执行文件将导致。所以下一步我们要弄清楚。然而,标记作为答案,解决了我正在研究的问题。