读取直到haskell中的流结束 [英] Read until end of stream in haskell
问题描述
我对Haskell还是很陌生,我想一直从控制台读取行,直到流结束,并输出大写形式的所有内容.到目前为止,我已经
I'm fairly new to Haskell, and I'd like to keep reading lines from the console until the end of the stream, and outputting everything I get in upper case. So far, I've got
import Data.Char
main = myLoop
myLoop = do inp <- getLine
if (inp == "x")
then putStrLn "Bye!"
else do putStrLn(map toUpper inp)
myLoop
但是,我似乎无法弄清楚如何避免if (inp == "x")
条件并将其替换为流结束条件.
However, I can't seem to figure out how to avoid the if (inp == "x")
condition and replace it with an end of stream condition.
简而言之,我正在寻找Haskell等效于C ++中的while (cin >> line)
In short, I'm looking for Haskell's equivalent to while (cin >> line)
in C++
推荐答案
这超出了您真正要求的范围,但是我经常使用这种模式:interact
:
This goes way beyond what you're really asking for, but I use this pattern a lot: interact
:
myFun :: String -> String
myFun = ...
main = interact (unlines . myFun . lines)
将对标准输入的每一行执行您的函数myFun
,并将结果发送到标准输出.
Your function myFun
will be executed against every line of standard input and the result sent to standard output.
这篇关于读取直到haskell中的流结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!