Search code examples
haskellfunctional-programmingio

Haskell IO: Couldn't match type `IO' with `[]'


playerInstruction :: [String] -> Int -> [String]
playerInstruction mapList count = do
    if count == 0
        then printf "First direction : "
        else printf "Next direction : "
    inp <- getLine
    case inp of
        "Up" -> instructionProc mapList Up
        "Down" -> instructionProc mapList Down
        "Left" -> instructionProc mapList Leftx
        "Right" -> instructionProc mapList Rightx
        otherwise -> playerInstruction mapList count

This is a part of the code of a project I am working on. This is a function to take in the player's instruction and redirect the player to another function. mapList is a list of String which supposed to contain the changes of the map step by step. count is the variable to count the number of instructions the player has input. The problem I am encountering is that the compiler is saying the line

inp <- getLine

having an error. With a message as stated in the topic. I am not sure how to fix this problem. Please feel free to see this link: Haskell IO: Reading the whole text file if you want to know more about this project. Thank you.


Solution

  • You need to declare the function as IO in the signature:

    playerInstruction :: [String] -> Int -> IO [String]