I am trying to read a char from a file being passed in via the stdinput in ocaml. I was wondering why I keep getting a unit when I can print_char the char that I receive in the following code. Also, I am trying to make a list of these characters which will not work as of now.
let () =
let charList = [] in
let inchar = open_in Sys.argv.(1) in
try
while true do
let c = Char.uppercase(input_char inchar) in
print_char c; print_int (List.length charList);c::charList
done
with End_of_file -> close_in inchar;(*; print_int (List.length charList); printTest charList;*)
;;
Your code does not modify
charList, socharListremains an empty list throughout the whole execution.Moreover, if you define
let charList = []thencharListis immutable.You might try something along these lines:
Some more comments:
input_line.Sys.argv.(1)and the standard input, but those are two distinct things