Haskell: 无法将预期类型“[a0]”与实际类型“([char], [int])”相匹配
Haskell: Couldn't match expected type '[a0]' with actual type '([char], [int])'
我的错误信息如下:' Couldn't match expected type '[a0]' with actual type '([char], [Int])' In the first argument of 'zip'
我正在尝试进行 运行 长度编码,例如
编码“aaaaabbbbbcc”
[('a',5),('b',4),('c',2)]
我的代码是这样的:
encode [] = []
encode ls = zip((map head list), (map length list))
where list = runs ls
'runs'函数returns[String],例如
运行s“aaaaabbbbbcc”
["aaaaa","bbbb","cc"]
我不知道如何解决它,任何帮助或解释将不胜感激!
您正在尝试使用 C-style 语法来调用 zip
,这被解释为 zip
获取单个元组作为其参数,而不是您想要的两个列表。
encode ls = zip (map head list) (map length list)
where list = runs ls
您可以在单个映射中执行此操作,其中对于 runs ls
的每个项目,您将其映射到一个 2 元组:
encode = map (\x -> (head x, length x)) . runs
或者我们可以将 lambda 表达式重写为:
encode = map ((,) <$> head <*> length) . runs
或者,如 , with (&&&) :: Arrow a => a b c -> a b c' -> a b (c, c')
:
import Control.Arrow((&&&))
encode = map (head <strong>&&&</strong> length) . runs
我的错误信息如下:' Couldn't match expected type '[a0]' with actual type '([char], [Int])' In the first argument of 'zip'
我正在尝试进行 运行 长度编码,例如
编码“aaaaabbbbbcc”
[('a',5),('b',4),('c',2)]
我的代码是这样的:
encode [] = []
encode ls = zip((map head list), (map length list))
where list = runs ls
'runs'函数returns[String],例如
运行s“aaaaabbbbbcc”
["aaaaa","bbbb","cc"]
我不知道如何解决它,任何帮助或解释将不胜感激!
您正在尝试使用 C-style 语法来调用 zip
,这被解释为 zip
获取单个元组作为其参数,而不是您想要的两个列表。
encode ls = zip (map head list) (map length list)
where list = runs ls
您可以在单个映射中执行此操作,其中对于 runs ls
的每个项目,您将其映射到一个 2 元组:
encode = map (\x -> (head x, length x)) . runs
或者我们可以将 lambda 表达式重写为:
encode = map ((,) <$> head <*> length) . runs
或者,如 (&&&) :: Arrow a => a b c -> a b c' -> a b (c, c')
:
import Control.Arrow((&&&))
encode = map (head <strong>&&&</strong> length) . runs