【问题标题】:Counting word occurences from an external file and display them using Erlang?计算来自外部文件的单词出现次数并使用 Erlang 显示它们?
【发布时间】:2013-12-16 22:33:42
【问题描述】:

我无法完全理解如何计算一个元素在文本文件中出现的次数。我可以像这样读取文件-

-module(list3).

-export([readlines/1]).

readlines(FileName) ->
{ok, Device} = file:open(FileName, [read]),
try get_all_lines(Device)
  after file:close(Device)
end.

get_all_lines(Device) ->
case io:get_line(Device, "") of
    eof  -> [];
    Line -> Line ++ get_all_lines(Device)
end.

我会将文本文件传输到字符串列表中,并且我知道 list:length 或 foldl 通常会计算列表中的元素数量,返回总体上有多少单词,但是,我想列出文件中的单词以及它出现了多少次。这会通过模式匹配来完成吗?我目前能想到的唯一解决方案需要大量代码。

任何提示将不胜感激。

【问题讨论】:

  • 我的措辞可能不正确,所以请随时指出我应该改变的地方。

标签: file count erlang


【解决方案1】:
猜你喜欢
  • 2013-03-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
相关资源
最近更新 更多