Returns values ​​from a file - ocaml

I am trying to read a file and return the element read from the file as input to another function.

How can I return a value when I read from a file? I tried everything I know and still hopelessly lost.

My code is as follows:

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 fx = 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 fl ;; prompt: read;; function call outputs: - : unit = () 

My file contains a line, which is the code needed to enter another function.

Please, help. I am not sure where I am going wrong.

Thanks.

+4
source share
1 answer

If multiple expressions are grouped together using ; , the value of the entire expression will be the value of the last expression in the sequence.

So, if you have something like ((x ^ line) :: l); close_in ic ((x ^ line) :: l); close_in ic , the value of this expression will be the value of close_in ic , which is equal to () .

Obviously not what you want. To make ((x ^ line) :: l) result of the whole expression, you must put it after close_in ic .

+4
source

All Articles