0

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

1
  • map f xs will always be a list with the same length as xs, no matter what function f is. Commented Nov 25, 2020 at 13:50

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.