简体   繁体   中英

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!!

You can use mapM:: (Traversable t, Monad m) => (a -> mb) -> ta -> m (tb) 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)

You can use traverse:: (Traversable t, Applicative f) => (a -> fb) -> ta -> f (tb) . In your code that looks like this:

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

The do notation solutions provided are quite normal, and easy for a beginner to understand. But with more experience, you might also consider using applicative style, to make it clearer (to both the reader and the compiler) that the handling of the player and the bullets are independent:

instance Renderable Player where
  render (MkPlayer pos rad bults _) = liftA2 go
      (displayimg pos rad "./images/player.bmp") 
      (traverse render bults)
    where go playerpic bulletpics = pictures $ playerpic : bulletpics

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM