functionhaskelliorenderinggloss

Trouble with IO objects Haskell


For uni I have this project where i need to program a simple game in haskell. Right now I'm facing the following problem:

instance Renderable Player where
  render (MkPlayer pos rad bults _) = do playerpic  <- displayimg pos rad "./images/player.bmp"
                                         bulletpics <- ...
                                         return $ pictures (playerpic:bulletpics)

at the ... i need a function f :: [Bullet] -> IO [Picture]

where the function producing a picture for the bullet object is :

render :: Bullet -> IO Picture

is there a way to create the function I need. I've been toying around on paper with monads and functors but cannot find a way to get this done. Any help at all with this is greatly appreciated!!


Solution

  • You can use mapM :: (Traversable t, Monad m) => (a -> m b) -> t a -> m (t b) for this:

    instance Renderable Player where
      render (MkPlayer pos rad bults _) = do
        playerpic  <- displayimg pos rad "./images/player.bmp"
        bulletpics <- mapM render bults
        return $ pictures (playerpic:bulletpics)