【问题标题】:exception error: no function clause异常错误:没有函数子句
【发布时间】:2018-09-03 13:21:25
【问题描述】:

我已按原样添加代码。它可以用于我在 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-export([main/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([]) -> [].     

【问题讨论】:

  • tokenList 是一个原子,而不是一个变量,所以用TokenList 替换它。
  • 你使用的是哪个版本的 Erlang?
  • 我使用的是 Eshell V9.2,从 tokenList 更改为 TokenList 会出现同样的错误。
  • 文件内容是什么?
  • 这是一个文本文件

标签: erlang


【解决方案1】:

unicode:characters_to_list 返回了一些错误。

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

并且 string: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.

【讨论】:

  • 我使用的是 Eshell V9.2,从 tokenList 更改为 TokenList 会出现同样的错误。
  • 关于如何改进这 3 个方面的任何建议
  • @RustHinges,你记得重新编译你的模块吗?只修改源代码是不够的。
猜你喜欢
  • 2021-04-03
  • 2015-01-14
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 2015-06-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多