异常错误:没有函数子句

时间:2018-03-25 08:16:48

标签: erlang

我已经添加了代码。它可用于我在Erlang中做一些工作的任何文本,我收到一条错误消息,我已在下面包含。

exception error: no function clause matching string:to_lower({error,[80,75,3,4,20,0,6,0,8,0,0,0,33,0,2020],                                                                     <<210,108,90,1,0,0,32,5,0,0,19,0,8,2,91,67,111,110,116,
                                                                        101,110,116,95,84,121,...>>}) (string.erl, line 2084)
     in function  word_sort:readlines/1 (word_sort.erl, line 17).

我还在下面提供了我的代码摘录,如果我能指出错误的地方,我将不胜感激。

enter code here -module(word_sort)。 enter code here - 出口([主/ 1])

-export([unique/2]).
-export([sort/1]).
-export([readlines/1]).
-export([wordCount/3]).


% ========================================================== %
%  Load the file and create a list %
% ========================================================== %

readlines(FileName) ->
    io:format("~nLoading File : ~p~n", [FileName]),
    {ok, File} = file:read_file(FileName),
    Content = unicode:characters_to_list(File),
    TokenList = string:tokens(string:to_lower(Content), " .,;:!?~/>'<{}£$%^&()@-=+_[]*#\\\n\r\"0123456789"),
    main(TokenList).

% ========================================================== %
% Scan through the text file and find a list of unique words %
% ========================================================== %
main(TokenList) ->                                  
    UniqueList = unique(TokenList,[]),           
    io:format("~nSorted List : ~n"),
    SortedList = sort(UniqueList),          % Sorts UniqueList into SortedList%
    io:format("~nSorted List : "),

    io:format("~nWriting to file~n"),
    {ok, F} = file:open("unique_words.txt", [write]),
    register(my_output_file, F),
    U = wordCounter(SortedList,TokenList,0),
    io:format("~nUnique : ~p~n", [U]),
    io:fwrite("~nComplete~n").

wordCounter([H|T],TokenList,N) ->
    %io:fwrite("~p \t:  ~p~n", [H,T]),
    wordCount(H, TokenList, 0),
    wordCounter(T,TokenList,N+1);

wordCounter([], _, N) -> N.

% =============================================================%
%Word count takes the unique word, and searches the original list for occurrences of that word%
%==============================================================%
wordCount(Word,[H|T],N) ->
    case Word == H of           % checks to see if H is in Seen List
        true -> wordCount(Word, T, N+1);              % if true, N_Seen = Seen List
        false -> wordCount(Word, T, N)       % if false, head appends Seen List.
    end;

wordCount(Word,[],N) -> 
    io:fwrite("~p   \t:  ~p ~n", [N,Word]),
    io:format(whereis(my_output_file), "~p   \t: ~p ~n", [N,Word]).

%=================================================================================

unique([H|T],Seen) ->                       % Accepts List of numbers and Seen List
    case lists:member(H, Seen) of           % checks to see if H is in Seen List
        true -> N_Seen = Seen;              % if true, N_Seen = Seen List
        false -> N_Seen = Seen ++ [H]       % if false, head appends Seen List.
    end,
    unique(T,N_Seen);                       % calls uniques with Tail and Seen List.

%=================================================================================

unique([],Seen) -> Seen.                    

sort([Pivot|T]) ->                          
    sort([ X || X <- T, X < Pivot]) ++     
    [Pivot] ++                              
    sort([ X || X <- T, X >= Pivot]);

sort([]) -> [].     

1 个答案:

答案 0 :(得分:1)

unicode:characters_to_list返回了一些错误。

变量'内容'包含错误消息而不是数据。

字符串:to_lower()获取错误消息作为参数而不是字符串。

您只需检查characters_to_list返回给您的内容。

readlines(FileName) ->
   io:format("~nLoading File : ~p~n", [FileName]),
   {ok, File} = file:read_file(FileName),
   case  unicode:characters_to_list(File) of
       Content when is_list(Content) ->
              LCcontent = string:to_lower(Content),
              TokenList = string:tokens(LCcontent, 
                              " .,;:!?~/>'<{}£$%^&()@-=+_[]*#\\\n\r\"0123456789"),
              main(TokenList);
       Err ->
           io:format("Cannot read file, got some unicode error ~p~n", [Err])
   end.