How can I print the type (Either String (IO String))?

It's a very, very long story, and I won't bore you, but I mostly managed to get into a situation where I needed to type a type Either String (IO String)

. Any help?

+3


source to share


2 answers


The solution is one liner ....

either print (print =<<)

      



If you want to differentiate whether it was Left

or Right

is a little more important, see @jamsihdh's answer.

Note that this cannot be done by an instance Show

, as nothing can be purely observable about the values โ€‹โ€‹of the type IO a

.

+13


source


The solution is not one liner ....

The monad is IO

not an instance Show

, so you cannot just use print

. In fact, the value in the IO monad must be received first.

You can view the value x::Either String (IO String)

by placing it in your body.

case x of
    Left s -> putStrLn ("Left " ++ show s)
    Right getVal -> do
             s <- getVal
             putStrLn ("Right (IO " ++ show s ++ ")")

      

and it has to resolve and print the value.




Edit -

I was misunderstood by @luqui, :) which is great because I learned a thing or two ....

Of course, now I have to go one step further and expose one liner with the corresponding left and right notation. :)

either (print . ("Left " ++)) ((print =<<) . fmap ("Right IO " ++))

      

+7


source







All Articles