如何从先前的操作中获取输出并使用>>=haskell进行打印?
在shell中,它就像,
echo "hello world" | { read test; echo test=$test; }
Run Code Online (Sandbox Code Playgroud)
在haskell,我正在寻找类似的东西,
putStrLn "hello world" >>= {x <- getArgs; print x}
Run Code Online (Sandbox Code Playgroud)
getArgs stdin必须从putStrLn的stdout中获取输入.
编辑#1,Alexey和aochagavia,感谢您的投入.这有效.
x :: IO String
x = return "hello world"
main = do
x >>= print
Run Code Online (Sandbox Code Playgroud)
不,>>=与stdout没有任何关系.您可以使用静默包中的capture_函数:
do x <- capture_ (putStrLn "hello world")
print x
Run Code Online (Sandbox Code Playgroud)
或者只是capture_ (putStrLn "hello world") >>= print.
| 归档时间: |
|
| 查看次数: |
255 次 |
| 最近记录: |