我在尝试在 Haskell 的守卫中使用守卫时遇到了一个问题,我试图找出是否(不可能)这样做,但我在互联网上找不到任何答案,我希望你们可能知道答案。这是我在尝试编译代码时遇到的错误:
Assignment2.hs:134:75: parse error on input '|'
我的代码:
verticals :: Board -> (Row, Row, Row)
verticals ((a,b,c),(d,e,f),(g,h,i)) = ((a,d,g),(b,e,h),(c,f,i))
symbolToPlayer :: Field -> Player
symbolToPlayer X = P1
symbolToPlayer O = P2
showTime :: Board -> Maybe Player
showTime ((a,b,c),(d,e,f),(g,h,i))
| a==b && a==c = Just (symbolToPlayer a)
| d==e && d==f = Just (symbolToPlayer d)
| g==h && g==i = Just (symbolToPlayer i)
| otherwise = False
hasWinner :: Board -> Maybe Player
hasWinner b@((a,b,c),(d,e,f),(g,h,i))
| showTime b -> spot of error
| showTime (verticals b)
| a==e && a==i = Just (symbolToPlayer a)
| c==e && c==g = Just (symbolToPlayer c)
| otherwise = Nothing
该代码用于井字游戏,这部分代码应该找出哪个玩家赢了;类型Board是自定义的,输入示例:hasWinner ((X,O,X),(X,O,O),(X,X,O)) (输出应该是:只是P1)。
谢谢您的帮助!