1

在同一个代码块中使用 2 个“end”似乎总是有问题,例如:

 Worker = fun (File) ->
 {ok, Device} = file:read_file([File]),
 Li = string:tokens(erlang:binary_to_list(Device), "\n"),
 Check = string:join(Li, "\r\n"),
 FindStr = string:str(Check, "yellow"),
 if
  FindStr > 1 -> io:fwrite("found");
  true -> io:fwrite("not found")
 end,
end,

消息是“之前的语法错误:'end'”

4

2 回答 2

5

您需要删除结束之间的逗号。

Worker = fun (File) ->
 {ok, Device} = file:read_file([File]),
 Li = string:tokens(erlang:binary_to_list(Device), "\n"),
 Check = string:join(Li, "\r\n"),
 FindStr = string:str(Check, "yellow"),
 if
  FindStr > 1 -> io:fwrite("found");
  true -> io:fwrite("not found")
 end
end,
于 2013-01-23T09:26:08.027 回答
2

规则很简单——所有“陈述”都必须以逗号开头,除非它们恰好是最后一个。

您的if表达式是fun传递给的块 ()中的最后一个foreach。这意味着它不需要尾随,.

所以

  end
end,

是你需要的。一个更简单的例子:

L = [1, 2, 3, 4],
lists:foreach(
  fun(X) -> 
     Y = 1, 
     if 
       X > 1 -> io:format("then greater than 1!~n");
       true  -> io:format("else...~n")
     end 
   end, 
   L
 )
于 2013-01-23T09:23:00.157 回答