How do I convert the below Haskell do notation to the bind (>>=)
notation?
rev2lines :: IO ()
rev2lines = do line1 <- getLine
line2 <- getLine
putStrLn (reverse line2)
putStrLn (reverse line1)
I am a Haskell beginner with decent knowledge and I tried something like
getLine >>= (\line1 -> getLine >>= (\line2 -> putStrLn $ reverse(line2)))
but I am not able to include the print statement for the other line i.e. line1. Kindly help me to understand this concept properly.
Do notations are simply designed to handle the IO operations in Haskell. To represent the do notation, we can simply use the 'do' keyword; after this, we can write our code that we want to execute. In most common general coding practice for Haskell, it usually comes up with the main module.
In Haskell, there is an operator bind, or ( >>= ) that allows for this monadic composition in a more elegant form similar to function composition. halve :: Int -> Maybe Int halve x | even x = Just (x `div` 2) | odd x = Nothing -- This code halves x twice.
What is a Monad? A monad is an algebraic structure in category theory, and in Haskell it is used to describe computations as sequences of steps, and to handle side effects such as state and IO. Monads are abstract, and they have many useful concrete instances. Monads provide a way to structure a program.
You're almost there: you need to use >>
.
getLine >>= (\line1 ->
getLine >>= (\line2 ->
putStrLn (reverse line2) >>
putStrLn (reverse line1)
))
Note that >> ...
is equivalent to >>= (\_ -> ...)
, so you can also use that if you prefer.
Similarly, your block
do line1 <- getLine
line2 <- getLine
putStrLn (reverse line2)
putStrLn (reverse line1)
is equivalent to
do line1 <- getLine
line2 <- getLine
_ <- putStrLn (reverse line2)
putStrLn (reverse line1)
Essentially, any entry in the block (but the last one) which has no explicit <-
uses >>
(or, if you prefer, has an implicit _ <-
in front).
Assuming you meant
rev2lines = do
line1 <- getLine
line2 <- getLine
putStrLn (reverse line2)
putStrLn (reverse line1)
the desugaring looks like
rev2lines =
getLine >>= \line1 ->
getLine >>= \line2 ->
putStrLn (reverse line2) >>
putStrLn (reverse line1)
which parses as
rev2lines =
getLine >>= (
\line1 -> getLine >>= (
\line2 -> (putStrLn (reverse line2)) >> (putStrLn (reverse line1))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With