如何在Haskell中查找字符串中的字符频率? [英] How to find the frequency of characters in a string in Haskell?
问题描述
例如,如果我输入单词happy结果将是
h 1
a 1
p 2
y 1
如果可以按照ASCII顺序排序,那就太棒了。
我知道我需要使用计数功能,任何其他提示将不胜感激。
编辑:所有的答案都很棒,只有我在Haskell这样一个初学者,我实际上并不了解他们在做什么。
最简单的解决方案是使用 Data.Map
来存储从字符到频率的中间映射。然后,您可以使用 fromListWith
。由于 Data.Map
是排序的,因此您可以免费获得ASCII码顺序。
λ> :m + Data.Map
λ>让input =happy
λ> toList $ fromListWith(+)[(c,1)| ('',1),('',1),(''',1),('',1),(''',1) c $ c>
这里发生了什么?
这个想法是使用字符作为键和频率作为值来构建 Data.Map
(树形图) p>
首先,我们接收输入字符串并为每个字符的元组添加一个 1
来表示一个出现。
λ> [(c,1)| ('',1),(''',1),('',1),(''',1) ]
接下来,我们使用 fromListWith
来构建通过将每个键值对重复插入到映射中,从这些键值对中分类映射。我们还给它一个函数,当一个键已经在地图上时,它将被使用。在我们的例子中,我们使用(+)
,这样当多次看到一个角色时,我们将计数添加到现有的总和中。
最后,我们使用 toList
来将映射转换回键值元组列表。
How can I count the frequency of characters in a string and then output them in sort of a table?
For example, if I input the word "happy" the result would be
h 1
a 1
p 2
y 1
If this could be ordered in ASCII order too that would be brilliant.
I know I need to use the count function, any other hints would be appreciated.
EDIT: All the answers are brilliant, only I'm such a beginner at Haskell that I don't actually understand what they are doing.
The simplest solution is to use a Data.Map
to store the intermediate mapping from character to frequency. You can then construct the counts easily using fromListWith
. Since Data.Map
is sorted, you get them in ASCII order for free.
λ> :m + Data.Map
λ> let input = "happy"
λ> toList $ fromListWith (+) [(c, 1) | c <- input]
[('a',1),('h',1),('p',2),('y',1)]
So what's happening here?
The idea is to build a Data.Map
(a tree map) using the characters as keys and the frequencies as values.
First, we take the input string and make tuples of each character with a 1
to indicate one occurrence.
λ> [(c, 1) | c <- input]
[('h',1),('a',1),('p',1),('p',1),('y',1)]
Next, we use fromListWith
to build a sorted map from these key-value pairs by repeatedly inserting each key-value pair into a map. We also give it a function which will be used when a key was already in the map. In our case, we use (+)
so that when a character is seen multiple times, we add the count to the existing sum.
Finally we covert the map back into a list of key-value tuples using toList
.
这篇关于如何在Haskell中查找字符串中的字符频率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!