Erlang:之前的语法错误:“,”word“

时间:2016-10-16 12:52:52

标签: erlang syntax-error

我有以下功能:

search(DirName, Word) ->
  NumberedFiles = list_numbered_files(DirName),
  Words = make_filter_mapper(Word),
  Index = mapreduce(NumberedFiles, Words, fun remove_duplicates/3),
  dict:find(Word, Index).

list_numbered_files(DirName) ->
  {ok, Files} = file:list_dir(DirName),
  FullFiles = [ filename:join(DirName, File) || File <- Files ],
  Indices = lists:seq(1, length(Files)),
  lists:zip(Indices, FullFiles). % {Index, FileName} tuples

make_filter_mapper(MatchWord) ->
  fun (_Index, FileName, Emit) ->
    {ok, [Words]} = file:consult(FileName), %% <---- Line 20
    lists:foreach(fun (Word) ->
      case MatchWord == Word of
        true -> Emit(Word, FileName);
        false -> false
      end
    end, Words)
  end.

remove_duplicates(Word, FileNames, Emit) ->
  UniqueFiles = sets:to_list(sets:from_list(FileNames)),
  lists:foreach(fun (FileName) -> Emit(Word, FileName) end, UniqueFiles). 

但是,当我调用搜索(Path_to_Dir,Word)时,我得到:

Error in process <0.185.0> with exit value:
{{badmatch,{error,{1,erl_parse,["syntax error before: ","wordinfile"]}}},
 [{test,'-make_filter_mapper/1-fun-1-',4,[{file,"test.erl"},{line,20}]}]}

我不明白为什么。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

Words变量将匹配列表的内容,这可能不仅仅是一个元组,而是许多元组。请尝试匹配{ok, Words}而不是{ok, [Words]}

答案 1 :(得分:0)

除了function file:consult/1可能返回多个元素的列表这一事实,你应该用{ok,[Words]}替换{ok,Words}(期望一个元素的列表=单词),它实际上返回一个语法错误意味着在您正在阅读的文件中,存在语法错误。

请记住,该文件应仅包含有效的erlang术语,每个术语都以点结尾。最常见的错误是忘记一个点或用逗号替换它。