简体   繁体   English

Haskell:无法将预期类型“[a0]”与实际类型“([char], [int])”相匹配

[英]Haskell: Couldn't match expected type '[a0]' with actual type '([char], [int])'

My error message is as follows: ' Couldn't match expected type '[a0]' with actual type '([char], [Int])' In the first argument of 'zip'我的错误信息如下:' Couldn't match expected type '[a0]' with actual type '([char], [Int])' In the first argument of 'zip'

I'm trying to do run length encoding, eg我正在尝试进行游程编码,例如

encode "aaaaabbbbcc"编码“aaaaabbbbbcc”
[('a',5),('b',4),('c',2)] [('a',5),('b',4),('c',2)]

My code is this:我的代码是这样的:

encode [] = []
encode ls = zip((map head list), (map length list))
 where list = runs ls 

The 'runs' function returns [String], eg 'runs' function 返回 [String],例如
runs "aaaaabbbbcc"运行“aaaaabbbbbcc”
["aaaaa","bbbb","cc"] [“aaaaa”,“bbbb”,“抄送”]

I don't know how to fix it, any help or explanation would be appreciated!我不知道如何解决它,任何帮助或解释将不胜感激!

You are trying to use C-style syntax to call zip , which is interpreted as zip getting a single tuple as its argument, rather than the two lists you intended.您正在尝试使用 C 风格的语法来调用zip ,它被解释为zip获取单个元组作为其参数,而不是您想要的两个列表。

encode ls = zip (map head list) (map length list)
   where list = runs ls

You can do this in a single mapping where for each item of the runs ls , you map it to a 2-tuple:您可以在单个映射中执行此操作,其中对于runs ls的每个项目,您将其 map 转换为 2 元组:

encode = map (\x -> (head x, length x)) . runs

or we can rewrite the lambda expression to:或者我们可以将 lambda 表达式重写为:

encode = map ((,) <$> head <*> length) . runs

or, as @leftroundabout says , with (&&&):: Arrow a => ab c -> abc' -> ab (c, c') :或者,正如@leftroundabout 所说,使用(&&&):: Arrow a => ab c -> abc' -> ab (c, c')

import Control.Arrow((&&&))

encode = map (head &&& length) . runs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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