我開始學習OCaml,並且一直困擾着一個簡單的問題。 我想用一個簡單的循環來計算我在6行文件中的字母數。 這裏是我的代碼:在OCaml中使用長度函數
let fi = open_in "example";;
let string = "";;
let nb_carac = 0;;
for i = 0 to 6 do
string = input_line fi;
nb_carac = (nb_carac + String.length(string));
done;;
問題是,它似乎並沒有什麼指望,直接達到EOF
。 我收到以下錯誤信息:
Warning 10: this expression should have type unit.
Warning 10: this expression should have type unit.
Exception: End_of_file.
我真的不明白。我已經單獨嘗試了每條線,並且他們都工作。我究竟做錯了什麼?
謝謝!
如果下面的後回答您的問題請[點擊接受檢查標誌](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)。如果它仍然不能回答你的問題,請在你的問題中添加更多細節,這樣有人可以回答它。當您將答案標記爲已接受時,它會浮動到頂部,以便其他讀者可以儘快看到正在工作的答案,答案的作者將被授予一些聲望點。 – 2015-04-02 13:26:06