Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No instance for (Show (IO ())) arising from a use of ‘print’

Tags:

haskell

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']?

like image 397
granmirupa Avatar asked May 09 '16 15:05

granmirupa


1 Answers

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)
like image 157
Lee Avatar answered Nov 10 '22 00:11

Lee