0

我在尝试在 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)。

谢谢您的帮助!

4

2 回答 2

6

您的程序中至少有 2 个严重的语法错误和一种类型错误。

语法错误:

    | showTime b        -> spot of error
    | showTime (verticals b)

这里缺少右手边。一般形式是

    | guard = expression

没有表情,你无法逃脱。编译器等待'=',但找到'|' 下一个守卫的,所以它知道它的错误。

类型错误在这里:

    | g==h && g==i = Just (symbolToPlayer i)
    | otherwise = False

您必须决定该函数是返回Bool还是返回Maybe Player

于 2013-10-02T22:53:12.587 回答
1

这个怎么样?

import Control.Monad (msum)
import Control.Applicative ((<*>), pure)

data Player = P1 | P2 | None deriving (Eq, Show)

data Field = X | O | B deriving (Eq, Show)

type Board = ((Field, Field, Field)
             ,(Field, Field, Field)
             ,(Field, Field, Field))

symbolToPlayer :: Field -> Player
symbolToPlayer X = P1
symbolToPlayer O = P2
symbolToPlayer B = None

checkThree :: (Field,Field,Field) -> Maybe Player
checkThree (a,b,c)
    | a == b && a == c = Just $ symbolToPlayer a
    | otherwise        = Nothing

winHorizontal :: Board -> Maybe Player
winHorizontal (r1, r2, r3) = msum $ map checkThree [r1, r2, r3]

winVertical :: Board -> Maybe Player
winVertical ((a,b,c), (d,e,f), (g,h,i)) =
    msum $ map checkThree [(a,d,g), (b,e,h), (c,f,i)]

winDiagonal :: Board -> Maybe Player
winDiagonal ((a,_,c), (_,e,_), (g,_,i)) =
    msum $ map checkThree [(a,e,i), (c,e,g)]

hasWinner :: Board -> Maybe Player
hasWinner b = msum $ [winHorizontal, winVertical, winHorizontal] <*> pure b

Monad 和 Applicative 是你的朋友!

于 2013-10-03T21:29:07.253 回答