Alex Goft
Alex Goft

Reputation: 1144

No instance for (Num String) arising from the literal `1' in if

Consider the next piece of code -

pvp :: Board -> Int -> IO ()
pvp board player = do
  playerchoice <- prompt $ ("Player " ++  (show (player + 1)) ++ ", it's your turn:")
  let newboard = if player == 0
                   then put board X (read playerchoice)
                   else put board O (read playerchoice)
  case newboard of
      Nothing -> do
        putStrLn "Invalid move."
        pvp board player
      Just board' -> putStrLn "Valid move."

When i try to compile the script i get the following error-

 No instance for (Num String) arising from the literal 1'
 In the second argument of (==)', namely 1'

This is how i call pvp -

main = do
  playGame emptyBoard
  where
    playGame board = do 
      game_choise   <- prompt "Choose game type: (1) PvC (2) PvP"
      if game_choise == 1
        then putStrLn "1"
        else pvp board 0

Upvotes: 0

Views: 600

Answers (1)

epsilonhalbe
epsilonhalbe

Reputation: 15959

my most likely fix (guess) would be

main = do
  playGame emptyBoard
  where
    playGame board = do 
      game_choise <- prompt "Choose game type: (1) PvC (2) PvP"
      if read game_choise == (1 :: Int)
        then putStrLn "1"
        else pvp board 0

Upvotes: 3

Related Questions