I understand (somewhat) monads and understand that the operator <- will extract the value from the monad.
But how does it work with different types?
Typically, I have seen it being used to extract strings from IO monad. But in the example code below am not able to see why it fails in main 3rd line, complaining that it is expecting a type of IO int? How is the compiler infering that an IO int is needed?
Also what does it (<-
) do in the multWithLog
method?
import Control.Monad.Trans.Writer.Lazy
main = do
putStrLn $ show $ logNumber 3 -- prints WriterT (Identity (3,["Got Number: 3"]))
putStrLn $ show $ multWithLog -- prints WriterT (Identity (3,["Got Number: 3"]))
_ <- logNumber 3 -- fails with Couldn't match type ‘WriterT [String] Data.Functor.Identity.Identity’ with ‘IO’
-- Expected type: IO Int
-- Actual type: Writer [String] Int
putStrLn "test"
logNumber :: Int -> Writer [String] Int
logNumber x = writer (x, ["Got Number: " ++ show x])
multWithLog :: Writer [String] Int
multWithLog = do
a <- logNumber 3
--b <- logNumber 5
return a