提问人:Icenezz 提问时间:10/31/2019 更新时间:10/31/2019 访问量:121
Haskell有没有办法检查自上一个当前时间以来是否已经过去了一分钟?
Is there a way in Haskell to check if a minute has passed since the previous current time?
问:
我是 Haskell 的新手,我正在尝试设置一个机器人,如果一分钟过去了,它会给你分数
我尝试寻找获取当前时间的方法,并在此基础上添加一分钟,并在一分钟过去后检查何时等于一分钟,例如addUTCTime。
我希望它看起来就是这样
if(timeNow == timeInAMinute) then sendMsg ("You got a point added!") else return ()
我还没有找到任何办法,所以我希望有一些实际的方法可以解决这个问题。我仍然需要处理 IO 的东西。所以回到我的问题,有没有办法检查当前时间是否已经过去了一分钟?
答:
6赞
leftaroundabout
10/31/2019
#1
这基本上可以满足您的要求:
import Data.Time
main :: IO ()
main = do
t0 <- getCurrentTime
go t0
where go tl = do
t <- getCurrentTime
if t >= 60`addUTCTime`tl
then do
putStrLn "You got a point!"
go t
else go tl
当然,这种在递归循环中忙碌的等待并不完全有效。一个更简单、更好的解决方案只是
import Control.Concurrent (threadDelay)
import Control.Monad (forever)
main :: IO ()
main = forever $ do
threadDelay $ 60 * 10^6
putStrLn "You got a point!"
评论