[Haskell-beginners] IO and ghci

Friedrich Wiemer friedrichwiemer at gmail.com
Sun Apr 19 10:03:03 UTC 2015


Hi Mike
as you perhaps have noticed, you use slightly different notations here:

> file <- readFile "poem.txt"

> let f = readFile “poem.txt”     

You can run both versions in ghci, and if you take a look at the type
signatures:
λ> file <- readFile "poem.txt"
λ> :t file
file :: String

λ> let f = readFile "poem.txt"
λ> :t f
f :: IO String

You'll notice that f has type "IO String", whereas file only has type
"String".

So, to answer your second question: you can progress by using the "<-"
notation, as in your haskell source code.

To answer the first one: I'm not sure about ghci command prompt, but I
would expect, that it is equivalent to "main = do ...".
That is, the type error you got, is not due to ghci and main = do are
different, but because you used to different types.

To understand the "<-" notation, the example from Learn you a Haskell
was quite good for me:
Try to see types like "IO String" as an action or box, which can result
in a string. You can take the inner type out of the box, by using "<-".

Hope that helps,

cheers,
Friedrich

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150419/ef9ca300/attachment.sig>


More information about the Beginners mailing list