没有(Num String)的实例来自字面上的'1'如果

时间:2017-11-03 11:45:09

标签: haskell

考虑下一段代码 -

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."

当我尝试编译脚本时,我收到以下错误 -

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

这就是我所说的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

1 个答案:

答案 0 :(得分:3)

我最可能的修复(猜测)将是

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