It's a very, very long story, and I won't bore you with it, but basically, I managed to get myself in a situation in which I need to be able to print the type Either String (IO String)
. Any help?
Asked
Active
Viewed 1,267 times
3

undo_all
- 139
- 4
-
To avoid this situation in the future, I'd recommend reading [this answer](http://stackoverflow.com/questions/13134825/how-do-functors-work-in-haskell/13137359#13137359) I wrote about Functor, including the sections "fmap works on Either something" and "It's especially cool to use fmap on IO". – AndrewC Sep 26 '14 at 09:59
2 Answers
13
The solution is a one liner....
either print (print =<<)
If you want to demarcate whether it was Left
or Right
it's a bit more involved, see @jamsihdh's answer.
Note that this cannot be made a Show
instance, since nothing can be purely observed about values of type IO a
.

luqui
- 59,485
- 12
- 145
- 204
7
The solution is not a one liner....
The IO
monad isn't an instance of Show
, so you can't just use print
. In fact, the value in the IO monad has to be obtained first.
You can view the value of x::Either String (IO String)
by putting this in your main....
case x of
Left s -> putStrLn ("Left " ++ show s)
Right getVal -> do
s <- getVal
putStrLn ("Right (IO " ++ show s ++ ")")
and it should resolve and print the value.
Edit-
I've been proven wrong by @luqui, :), which is cool, because I learned something....
Of course now I need to go one step further and put out a one-liner with the appropriate Left and Right designation. :)
either (print . ("Left " ++)) ((print =<<) . fmap ("Right IO " ++))

jamshidh
- 12,002
- 17
- 31