user2999349
user2999349

Reputation: 879

haskell minimax / nested conditions and wheres

For an assignment I need to write a Minimax function on a Gametree provided to the function (as a tree of boards; Rose Board) and a player who's turn it is. However I'm getting this error about parse error on input '|'. Probably because I nested conditions and where statements but I'm not sure if I done this properly or if this is even possible (or should be done in a different way):

minimax :: Player -> Rose Board -> Rose Int --Rose Int = Int :> [Rose Ints]
minimax p rb = minimax' rb p
          where minimax' (b :> [rbs]) p0 | null rbs  = result
                                                where result | p0 == p   =  1
                                                             | otherwise = -1
                                         | otherwise = 0 :> (minimax' rbs (nextPlayer p0))

If someone could help me out it's much appreciated!

Best regards, Skyfe.

Upvotes: 1

Views: 1340

Answers (1)

fjh
fjh

Reputation: 13091

The simplest way to fix this is probably to use let instead of where:

minimax :: Player -> Rose Board -> Rose Int --Rose Int = Int :> [Rose Ints]
minimax p rb = minimax' rb p
          where minimax' (b :> [rbs]) p0 | null rbs  = let result | p0 == p   =  1
                                                                  | otherwise = -1
                                                       in result
                                         | otherwise = 0 :> (minimax' rbs (nextPlayer p0))

but you could also just use a conditional expression:

minimax :: Player -> Rose Board -> Rose Int --Rose Int = Int :> [Rose Ints]
minimax p rb = minimax' rb p
          where minimax' (b :> [rbs]) p0 | null rbs  = if p0 == p then 1 else -1
                                         | otherwise = 0 :> (minimax' rbs (nextPlayer p0))

Upvotes: 1

Related Questions