简体   繁体   English

计算字符串列表中的单词 Haskell

[英]Count words in list of strings Haskell

So i have to count every word in a list of strings in Haskell所以我必须计算 Haskell 中字符串列表中的每个单词

Example:例子:

Cwords ["one string random", "hello asd", "asd"] -> 6 cwords ["一个字符串随机", "hello asd", "asd"] -> 6

This is how i tried:这就是我尝试的方式:

Cwords :: [String] -> Int
Cwords = length . map words

Now it gives 3 for the same input现在它为相同的输入给出 3

For each element of the list, you want to find the length of the words , aka map (length. words) .对于列表的每个元素,您要查找words长度,也就是map (length. words) You'll then have an [Int] where each element is the length of the words in that element, so you want to sum it up:然后你会有一个[Int] ,其中每个元素都是该元素中单词的长度,所以你想总结一下:

cwords :: [String] -> Int
cwords = sum . map (length . words)

Note that functions must start with a lowercase letter.请注意,函数必须以小写字母开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM