【发布时间】:2020-07-29 21:03:20
【问题描述】:
大家好,我正在开发一个函数,它将棋盘游戏的棋盘输出为字符串。 目前我有它的工作,所以我可以输出没有任何碎片的板。
我正在尝试添加一个过滤器,以便如果当前 (x, y) 坐标在元组列表中 [Position, Col] 其中Position 是(Int, Int)。
如果(x, y) 在此列表中,那么我想检查颜色Col,然后相应地输出。
drawBoardCell :: (Int, Int) ->[(Position, Col)] -> String
drawBoardCell (x, y) pieces = do
let test = filter (\((a,b),_) -> a == x && b == y) pieces
if snd(test) == Black
then " b "
else if snd(test) == White
then " w "
else " . "
这是我到目前为止所尝试的并遇到错误:
Display.hs:47:14: error:
• Couldn't match expected type ‘(a0, Col)’
with actual type ‘[((Int, Int), Col)]’
• In the first argument of ‘snd’, namely ‘(test)’
In the first argument of ‘(==)’, namely ‘snd (test)’
In the expression: snd (test) == Black
Display.hs:49:19: error:
• Couldn't match expected type ‘(a1, Col)’
with actual type ‘[((Int, Int), Col)]’
• In the first argument of ‘snd’, namely ‘(test)’
In the first argument of ‘(==)’, namely ‘snd (test)’
In the expression: snd (test) == White
【问题讨论】:
-
filter的返回值是一个列表 ([a]) 所以test是一个列表 - 错误消息中的实际类型。另一方面,snd需要一个元组 - 错误消息中的 预期类型。这两种类型不匹配。
标签: list haskell filter tuples