Here is my code:
getMove :: Board -> Player -> IO (Maybe (Move, Board, Player))
completeUserTurn :: Player -> Board -> Maybe (IO (Board, Player))
completeUserTurn player board = do
m <- getMove board player --error is here
if isNothing m then
Nothing
else do
let (move, updatedBoard, updatedPlayer) = fromJust m
if isMoveValid board move then do
continue <- prompt $ displayToUserForPlayer updatedBoard updatedPlayer ++ "\n" ++ "Is this correct? (y/n): "
if continue == "y" then
return (updatedBoard, updatedPlayer)
else
completeUserTurn player board
else do
putStr "Invalid Move!\n"
completeUserTurn player board
Here is the error I am getting (On the indicated line):
Couldn't match expected type `Maybe t0'
with actual type `IO (Maybe (Move, Board, Player))'
In the return type of a call of `getMove'
In a stmt of a 'do' block: m <- getMove board player
In the expression:
do { m <- getMove board player;
if isNothing m then
Nothing
else
do { let ...;
.... } }
What is wrong? I though the <- would do the IO action and put the result in m? Why does it expect a Maybe then?
General advice
Once you’ve run an
IOoperation likegetMoveyour function has to have typeIO ????. Anything that does interaction with the outside world lives in the IO monad, so your function has to have typenot
The only way to have type
Maybe (IO ???)is to not actually do any IO:this function doesn’t actually do the
getLineand isn’t very useful, as you can check by doingin ghci: it never says Hooray. More useful would be
(Try
continue Trueandcontinue Falsein ghci)This one actually does the IO, so it has to have type
IO ???.Your code
Anyway, your function is better expressed as
Both edit #1 and edit #2 are because of the unavoidable change of type to
IO (Maybe ??).