【发布时间】:2018-06-16 05:17:51
【问题描述】:
我是新的 Haskell 学习者,正在尝试计算单词,但有错误。如何更改代码并显示这样的结果
countWords ["friend","she","she"]
>[("friend",1),("she",2)
这里是代码
Prelude Data.List> countWords xs = map(\w -> (head w, length w))
$group $ sort $ words xs
Prelude Data.List> countWords ["hello", "hello", "world"]
:101:13: 错误: • 无法将预期类型“Char”与实际类型“[Char]”匹配 • 在表达式中:“你好” 在‘countWords’的第一个参数中,即 '[“你好”,“你好”,“世界”]' 在表达式中:countWords ["hello", "hello", "world"]
:101:22: 错误: • 无法将预期类型“Char”与实际类型“[Char]”匹配 • 在表达式中:“你好” 在‘countWords’的第一个参数中,即 '[“你好”,“你好”,“世界”]' 在表达式中:countWords ["hello", "hello", "world"]
:101:31: 错误: • 无法将预期类型“Char”与实际类型“[Char]”匹配 • 在表达式中:“世界” 在‘countWords’的第一个参数中,即 '[“你好”,“你好”,“世界”]' 在表达式中:countWords ["hello", "hello", "world"]
谢谢
【问题讨论】:
-
words需要一个字符串,而不是字符串列表。你确定你需要words吗?输入似乎已经被拆分成单词。
标签: haskell