For instance, suppose I want to read a line and also ring a bell:
λ getLine >> putChar '\007'
How lang and dreary is the night when I am frae my Dearie.
-- Blip and `()`. The line is lost.
λ getLine >>= (\x -> putChar '\007' >> return x
I restless lie frae e'en to morn though I were ne'er sae weary.
"I restless lie frae e'en to morn though I were ne'er sae weary."
-- A line and also a blip side effect.
The idea seems to have much in common with const
, the only difference being that the values given are effectful, and are both executed, even though only the value of the first action is retained. (Unlike with >>
, which retains the value from the second.) What I mean exactly is this:
λ constM a b = a >>= \x -> b >> return x
Here is a more complex example, involving parsers from Text.ParserCombinators.ReadP
:
λ readP_to_S (many1 (munch1 (not . isSpace) `constM` skipSpaces ) `constM` eof) <$> getLine
How slow ye move, ye heavy hours.
[(["How","slow","ye","move,","ye","heavy","hours."],"")]
I wonder if this function is available in base
, or easily constructible from other functions in base
.
(<*) :: Applicative f => f a -> f b -> f a
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