Just for learning I did this on my console WinGHCi:
let ls = [putChar 'x',putChar 'y']
then if do:
head ls
the output is obviusly x (obviusly in the sense that I undestand why)
else if I do:
tail ls
I get this error:
No instance for (Show (IO ())) arising from a use of ‘print’
In a stmt of an interactive GHCi command: print it
Why? Shouldn't output y or [putChar 'y']?
This behaviour is explained here.
head ls has type IO () so GCHi executes the action and doesn't print the result ().
In contrast, tail ls has type [IO ()]. Since this is not an IO action, GCHi attempts to display it using print which has type:
print :: Show a => a -> IO ()
however since there is no Show instance for [IO ()] you get the error. If you want to evaluate the actions you can use sequence_:
sequence_ (tail ls)
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