haskell - hWaitForInput 如何工作?

标签 haskell

这段代码从stdin获取字符并将它们转换为字符串。使用 hWaitForInput 实现的输入存在超时。我已经包含了 tryIOError 函数来捕获 isEOFError。当程序运行并且没有发生输入(即正在等待输入)时,它会正确超时。然而,一旦输入一个字符,它就不再超时。无论输入等待时间是多少。

-- get string from stdin
getFrmStdIn :: Int -> IO String
getFrmStdIn timeout = do
  inAvail <- tryIOError $ hWaitForInput stdin timeout
  let tOut = either (const False) id inAvail
  if isLeft inAvail
    then return []
  else
    if not tOut
      then die "00 timed out"
    else
      (:) <$> hGetChar stdin <*> getFrmStdIn timeout

最佳答案

您使用的操作系统和 GHC 版本是什么?

据我所知,最新的 GHC 版本 hWaitForInput doesn't work on linux当超时不为 0 时根本不会。在 Windows 上,整个 IO 子系统为 "a bit lacking"也是。

关于haskell - hWaitForInput 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43409494/

相关文章:

haskell - Facebook 的小鸭子无法正确识别时间维度

Haskell:无法将预期类型 'IO t0' 与实际类型 'Integer' 匹配

haskell - 在 Haskell 中返回某种类型的空对象

haskell - StateT 与 ReaderT IORef 的异常处理

haskell - Haskell 中产品类型和元组的冗余

Haskell lambda 表达式和简单的逻辑公式

haskell - 在 2 维中喜结连理(原为 : tying the knot with a comonad)

Haskell - 由于我不明白的原因,非穷举模式

haskell - 使用 Haskell 查找偶数排列

scala - 它如何是自然的转变?