0

Haskell playIO 有以下类型:

 playIO:: Display   
-> Color    background color
-> Int  
-> world    --initial world 
-> (world -> IO Picture)  -- function to change world into a picture    
-> (Event -> world -> IO world) --event handler function
-> (Float -> world -> IO world) -- The function to update the world after the given time 
-> IO ()

一旦你playIO在里面调用main,它就会不断运行更新由world. 如果在处理事件的代码(参见代码注释)或更新世界的函数中发生了某些事情,并且您想输出一条消息(不一定是错误),使用什么方法不违反类型?是否必须打破功能playIO来显示我的信息,如果是这样,人们将如何做到这一点?

4

1 回答 1

3

例如,如果您想根据事件发出消息,则将该操作放入事件处理程序中。例如:

main :: IO ()
main = playIO black 100 world0 renderWorld handleEvent updateWorld

handleEvent evt w =
    do print event -- Right here, you are emitting a message!
       updateWorldWithEvent evt w
       putStrLn "I have updated the world, now time for breakfast."

请记住,该handleEvent操作可能会非常频繁地发生,因此请相应地选择您的输出。

于 2019-01-14T04:52:51.660 回答