user3638162
user3638162

Reputation: 431

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

I'm trying to map a drawing function to every element in a list. The function itself, drawMap, seems ok but when I use it I get the following error:

Couldn't match type `[]' with `IO'
Expected type: IO (IO Bool)
  Actual type: [IO Bool]
In the return type of a call of `drawMap'
In a stmt of a 'do' block: drawMap testMap 32 img screen
In the second argument of `($)', namely
  `do { screen <- SDL.setVideoMode 640 480 32 [SDL.SWSurface];
        img <- SDL.loadBMP "grass.bmp";
        drawMap testMap 32 img screen;
        SDL.flip screen;
        .... }'

From reading this I now understand that it's got something to do with what the function is returning, but I've got no idea at the moment how to fix it.

My code looks like this:

testMap = [Tile 0 0 1, Tile 0 1 1, Tile 0 2 1, Tile 0 3 1]

drawTile :: Int -> SDL.Surface -> SDL.Surface -> Tile -> IO Bool
drawTile tilesize img dst tile = applySurface (tileX tile * tilesize) (tileY tile * tilesize) img dst

drawMap :: [Tile] -> Int -> SDL.Surface -> SDL.Surface -> [IO Bool]
drawMap tiles tilesize img dst =
  map (drawTile tilesize img dst) tiles

main :: IO ()
main = SDL.withInit [SDL.InitEverything] $ do

  screen <- SDL.setVideoMode 640 480 32 [SDL.SWSurface]

  img <- SDL.loadBMP "grass.bmp"

  --drawTile 32 img screen (testMap !! 1)
  drawMap testMap 32 img screen

  SDL.flip screen

  mainLoop

Upvotes: 2

Views: 304

Answers (1)

ErikR
ErikR

Reputation: 52029

I think you want:

drawMap tiles tilesize img dst =
  mapM (drawTile tilesize img dst) tiles

mapM is from Control.Monad. The type of drawMap becomes:

drawMap :: [Tile] -> Int -> SDL.Surface -> SDL.Surface -> IO [Bool]

i.e. it is an IO action returning a list of Bool, not a list of IO actions.

Upvotes: 7

Related Questions