简体   繁体   中英

Count words in list of strings Haskell

So i have to count every word in a list of strings in Haskell

Example:

Cwords ["one string random", "hello asd", "asd"] -> 6

This is how i tried:

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

Now it gives 3 for the same input

For each element of the list, you want to find the length of the words , aka 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:

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

Note that functions must start with a lowercase letter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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