Haskell:立即从控制台读取输入字符,而不是在换行符之后 [英] Haskell: read input character from console immediately, not after newline

查看:101
本文介绍了Haskell:立即从控制台读取输入字符,而不是在换行符之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  main = do 
hSetBuffering stdin NoBuffering
c< - getChar

但它等到输入按下时,这不是我想要的。我想在用户按下后立即读取字符。



我在Windows 7上使用ghc v6.12.1。



编辑:我的解决方法是从GHC移动到WinHugs,它正确地支持这一点。

可能是一个bug:

http:// hackage .haskell.org / trac / ghc / ticket / 2189


以下程序重复输入的字符,直到按下转义键

  import IO 
import Monad
import Char

main :: IO()
main = do hSetBuffering stdin NoBuffering
inputLoop

inputLoop :: IO()
inputLoop = do i< - getContents
mapM_ putChar $ takeWhile((/ = 27)。ord)

由于hSetBuffering stdin NoBuffering行没有必要按下在击键之间输入密钥。此程序在WinHugs(sep 2006版本)中正常工作。但是,直到按下回车键后,GHC 6.8.2才会重复字符。这个问题在所有GHC可执行文件(ghci,ghc,runghc,runhaskell)中被复制,在Windows XP Professional上同时使用cmd.exe和command.com ...


I've tried this:

main = do
    hSetBuffering stdin NoBuffering 
    c <- getChar

but it waits until the enter is pressed, which is not what I want. I want to read the character immediately after user presses it.

I am using ghc v6.12.1 on Windows 7.

EDIT: workaround for me was moving from GHC to WinHugs, which supports this correctly.

解决方案

Might be a bug:

http://hackage.haskell.org/trac/ghc/ticket/2189

The following program repeats inputted characters until the escape key is pressed.

import IO
import Monad
import Char

main :: IO ()
main = do hSetBuffering stdin NoBuffering
          inputLoop

inputLoop :: IO ()
inputLoop = do i <- getContents
               mapM_ putChar $ takeWhile ((/= 27) . ord) i

Because of the hSetBuffering stdin NoBuffering line it should not be necessary to press the enter key between keystrokes. This program works correctly in WinHugs (sep 2006 version). However, GHC 6.8.2 does not repeat the characters until the enter key is pressed. The problem was reproduced with all GHC executables (ghci, ghc, runghc, runhaskell), using both cmd.exe and command.com on Windows XP Professional...

这篇关于Haskell:立即从控制台读取输入字符,而不是在换行符之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆