I want to sequentially compose two monad actions in Haskell, discarding any value produced by the second, and passing the argument to both actions. Currently I'm using a do-block like this:
ask = do
result <- getLine
putStrLn result
return result
I was hoping to write this a little more point free and neat, so I tried this:
ask' = getLine <* putStrLn
However, this doesn't even type check and the problem is that <*
does not transfer the result of the first action to the second. I want to chain the actions like >>=
does, but not change the result. The type should be (a -> m b) -> (a -> m c) -> (a -> m b)
, but Hoogle yields no suitable results. What would be an operator to achieve this function composition?
getLine >>= \x -> putStrLn x >> return x
in point free style. Have you asked lambdabot? It saysliftM2 (>>) putStrLn return =<< getLine
. – Moidoreflip (liftM2 (>>)) :: Monad m => (a -> m b) -> (a -> m c) -> (a -> m b)
- the actual type is slightly more generalized so it is somewhat hard to see. – Monauralpointfree
command line tool, and it could not handledo
, so I gave up. I ended up usingask = getLine >>= liftM2 (>>) putStrLn return
, which looks okay, thanks again! You can put this into an answer if you want, then I can mark it as solved. – Ferity