Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect last action's stdout using >>= in haskell

Tags:

haskell

pipe

How can i get the output from previous action and print it using >>= in haskell?

In shell, it is like,

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

In haskell, i am looking for something like,

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

getArgs stdin must take its input from putStrLn's stdout.

Edit#1, Alexey & aochagavia, thanks for your inputs. This works.

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

main = do
  x >>= print

1 Answers

No, >>= doesn't have anything to do with stdout. You can use capture_ function from the silently package:

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

or just capture_ (putStrLn "hello world") >>= print.

like image 176
Alexey Romanov Avatar answered Apr 02 '26 22:04

Alexey Romanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!