ocaml - 如何从 OCaml 中的文本文件中逐行读取?

标签 ocaml

这就是我到目前为止所拥有的。这不是你所需要的吗?我不断收到错误“错误:未绑定(bind)模块标准”

let r file =
    let chan = open_in file in
    Std.input_list (chan)

最佳答案

仅使用标准库的命令式解决方案:

let read_file filename = 
let lines = ref [] in
let chan = open_in filename in
try
  while true; do
    lines := input_line chan :: !lines
  done; !lines
with End_of_file ->
  close_in chan;
  List.rev !lines ;;
如果您有 Batteries-included库,您可以将文件读入 Enum.t 并按如下方式对其进行迭代:
let filelines = File.lines_of filename in
Enum.iter ( fun line -> (*Do something with line here*) ) filelines

关于ocaml - 如何从 OCaml 中的文本文件中逐行读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5774934/

相关文章:

c++ - 如何从类声明中正确导出 SWIG typedef?

ocaml - 为什么 Array.map 比尾递归映射(相当)快?

multithreading - 带有 OCAML 的 OpenMP

floating-point - OCaml是否具有类似C的round()和trunc()函数?

ocaml - 使用 ref 单元格的动态绑定(bind)

ocaml - 如何在 OCaml 中实现 lambda 演算?

functional-programming - 模式匹配返回数学表达式的字符串表示

ocaml - 在 2D 列表 (OCaml) 中组合具有相同头部的列表

macos - 错误 : Version 1. 11 沙丘不受支持

functional-programming - 将模块及其实例作为 OCaml 函数的参数