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!

upvote
  flag
Have you checked all your whitespaces are spaces and not tabs? Also the function has winner was -> instead of = – DiegoNolan
upvote
  flag
I wonder why you expect this to compile, and how it's supposed to work. Apparently you want to somehow evaluate the Maybe result of showTime? – leftaroundabout

2 Answers 11

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!

Not the answer you're looking for? Browse other questions tagged or ask your own question.