2015-07-11 33 views
4

如何從haskell中獲得前一個操作的輸出並使用>>=進行打印?使用>> =在haskell中重定向上次操作的stdout

殼,很是喜歡,

echo "hello world" | { read test; echo test=$test; } 

在Haskell,我期待這樣的事情,

putStrLn "hello world" >>= {x <- getArgs; print x} 

getArgs標準輸入必須從putStrLn的stdout其輸入。

編輯#1, Alexey & aochagavia,感謝您的意見。這工作。

x :: IO String 
x = return "hello world" 

main = do 
    x >>= print 
+0

您的代碼不能編譯。檢查'return'的類型簽名:'return :: Monad m => a - > m a'。也許你可以看看http://learnyouahaskell.com/input-and-output – aochagavia

+0

刪除'return' ...但仍然錯誤,我正在調試它 –

+0

你可以去http://chat.stackoverflow.com/rooms/83002/haskell-room在聊天中討論它 – aochagavia

回答

7

不,>>=與stdout沒有任何關係。您可以使用capture_函數從silently包:

do x <- capture_ (putStrLn "hello world") 
    print x 

或只是capture_ (putStrLn "hello world") >>= print

+0

編輯問題...您的輸入是有幫助的,謝謝Alex ... –