简体   繁体   中英

Can I use guards within guards in Haskell

I encountered a problem whilst trying to use guards within guards in Haskell, I tried finding out if it was(n't) possible to even do so but I couldn't find any answers on the Internet, I was hoping you guys might know the answer. This is the error I'm getting while trying to compile my code: Assignment2.hs:134:75: parse error on input '|'

my code:

    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

The code is for a game of tic-tac-toe, this part of the code is supposed to find out which player has won; the type Board is self-defined, example of input: hasWinner ((X,O,X),(X,O,O),(X,X,O)) (the output should be: Just P1).

Thanks for the help!

You have at least 2 grave syntax errors and one type error in your program.

Syntax errors:

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

Here the right hand side is missing. The general form is

    | guard = expression

You can't get away without an expression. The compiler waits for a '=', but finds the '|' of the next guard and so it knows its faulty.

The type error is here:

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

You must decide whether the function should return Bool or Maybe Player .

How about this?

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 and Applicative are your friends!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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