Ocaml 这里的行终止符应该是什么

Ocaml 这里的行终止符应该是什么,ocaml,Ocaml,我正在编写一个简短的脚本来读取脚本文件文件夹中的每个文件,并打印最后一个字的第一行-表示运行的可执行文件-ocaml,例如在usr/bin/ocaml #use "topfind" #require "str" (* fn to print binary file being called from hashbang line of script file *) let rec myfn (afile) = print_string (afile^": "); if (Sy

我正在编写一个简短的脚本来读取脚本文件文件夹中的每个文件,并打印最后一个字的第一行-表示运行的可执行文件-
ocaml
,例如在
usr/bin/ocaml

#use "topfind" 
#require "str"

(* fn to print binary file being called from hashbang line of script file *)
let rec myfn (afile) = 
    print_string (afile^": ");
    if (Sys.is_directory afile) then print_endline("This is a directory.");
    let ic = (open_in afile) in
    if in_channel_length(ic)==0 then print_endline("Zero length file."); exit 0
    let line = input_line(ic) in    (* error here *)
    if (Str.first_chars line 0 = "#") then
        let linelist = (Str.split (Str.regexp "/") line) in    
        let lastword = List.nth linelist ((List.length linelist) - 1) in 
        print_endline(lastword)
    else
        print_endline("Not a script file.") ;; 

(* to check all files in directory *)
let dir = "." in 
let files = Sys.readdir dir in 
Array.iter myfn files;; 
但是,它给出了以下错误:

File "firstline3.ml", line 13, characters 27-29:
Error: Syntax error

我试图用
替换
中的
或此行中没有终止符,但它没有帮助。问题是什么?如何解决。谢谢您的帮助。

您缺少
的括号,然后
表达式:

if in_channel_length ic =0 then ( print_endline "Zero length file."; exit 0 );
否则,解析器会将表达式读取为

(if in_channel_length ic = 0 then print_endline "Zero length file.");
exit 0;
因此,退出是无条件的

注意,您应该避免使用物理相等运算符,
=
,(尤其是在不可变值上)并使用
=

编辑:如果要返回,只需添加一个else分支

if in_channel_length ic = 0 then print_endline "Zero length file."
else ....

如果我把
退出0;,我猜在前一行的
退出0之后会缺少一个行终止符我得到
错误:上面一行中的未绑定值ic
。如果我将
中的
替换为
,我在那一行得到了
语法错误
。换成一个分号怎么样?这里有一个警告
警告21:这个语句永远不会返回(或类型不正确)。
但是函数只运行一个文件,不会对所有文件进行循环。实际上,我希望循环在这里继续,但Ocaml中没有
continue
关键字。实际上,我希望它退出fn,所以返回而不是退出程序。但是
Ocaml
没有
return
。实际上,还有其他错误。您应该检查
Str.first\u chars第0行返回的内容。它总是返回false。如果将
“#”
替换为
“#”
,则会出现类型不匹配错误-char vs string。您应该尝试打印
Str.first_chars“a word”0
。在单独的顶级实例中尝试。