How to sleep or delay the thread in Haskell?

我是研究僧i 提交于 2021-01-02 07:12:57

问题


This really shouldn't be so difficult to find an answer to, but alas I don't... I want to delay the next execution step in a do block. I have found the functions delay, sleep, nanosleep and usleep.

And also this question, that doesn't cover how to use any of these, however: Sleep in Haskell.

I am getting the same error for all of these, so probably I am doing something wrong fundamentally:

Variable not in scope: delay :: Integer -> IO a0

This is my test snippet:

main = do
  {
    putStrLn "line 1"
  ; delay 2
  ; putStrLn "line 2"
  }

Googling the error does not actually yield anything useful for some reason.


回答1:


Well, you have to import Control.Concurrent first:

import Control.Concurrent

threadDelay 1000000 --sleep for a million microseconds, or one second


来源:https://stackoverflow.com/questions/47816174/how-to-sleep-or-delay-the-thread-in-haskell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!