从文件返回值-ocaml

从文件返回值-ocaml,ocaml,Ocaml,我试图读取一个文件,并将从该文件读取的元素作为输入返回给另一个函数 读取文件时如何返回值?? 我尝试了我所知道的一切,但仍然毫无希望地迷失了方向 我的代码如下: let file = "code.txt";; let oc = open_out file in (* create or truncate file, return channel *) fprintf oc "%s\n" (play); (* write code to file returned from

我试图读取一个文件,并将从该文件读取的元素作为输入返回给另一个函数

读取文件时如何返回值?? 我尝试了我所知道的一切,但仍然毫无希望地迷失了方向

我的代码如下:

let file = "code.txt";;

let oc  = open_out file in    (* create or truncate file, return channel *)

    fprintf oc "%s\n" (play);   (* write code to file returned from calling (play) function *)   

    close_out oc    ;;


(*read from file*)


let read l=

    let f x =

        let ic  = open_in file in

        let line = input_line ic in  (* read line from in_channel and discard \n *)

            print_endline line;          (* write the result to stdout *)

                ((x ^ line) :: l);

            flush stdout;                

            close_in ic ;
    in
     f l
    ;;

prompt:  read;; function call outputs:

- : unit = ()
我的文件包含一个字符串,该字符串是另一个函数输入所需的代码

请帮忙。我不确定我会错在哪里


谢谢。

如果使用
将多个表达式排序在一起整个表达式的值是序列中最后一个表达式的值

所以如果你有类似于
((x^line)::l);ic中的close_
该表达式的值是ic中的
close_
的值,即
()

显然那不是你想要的。为了使
((x^line)::l)
成为整个表达式的结果,应将其放在ic中的
关闭\u之后