I am new Haskell learner, and trying to do count words, but there are errors. how can i change to code and show the result like this
countWords ["friend","she","she"]
>[("friend",1),("she",2)
here is code
Prelude Data.List> countWords xs = map(\w -> (head w, length w))
$group $ sort $ words xs
Prelude Data.List> countWords ["hello", "hello", "world"]
:101:13: error: • Couldn't match expected type ‘Char’ with actual type ‘[Char]’ • In the expression: "hello" In the first argument of ‘countWords’, namely ‘["hello", "hello", "world"]’ In the expression: countWords ["hello", "hello", "world"]
:101:22: error: • Couldn't match expected type ‘Char’ with actual type ‘[Char]’ • In the expression: "hello" In the first argument of ‘countWords’, namely ‘["hello", "hello", "world"]’ In the expression: countWords ["hello", "hello", "world"]
:101:31: error: • Couldn't match expected type ‘Char’ with actual type ‘[Char]’ • In the expression: "world" In the first argument of ‘countWords’, namely ‘["hello", "hello", "world"]’ In the expression: countWords ["hello", "hello", "world"]
Thank you
words
expects a single string, not a list of strings. Are you sure you needwords
at all? The input seems to be already split into words. – chi