
Hi, I have this in a .hs file and it works fine. That is I see lines printed to the screen when the .hs file is loaded and run from ghci. main = do file <- readFile "poem.txt" mapM_ putStrLn (lines file) Experimenting at the ghci command line with let f = readFile “poem.txt” the above is fine. But doing lines f gives <interactive>:52:7: Couldn't match type ‘IO String’ with ‘[Char]’ Expected type: String Actual type: IO String In the first argument of ‘lines’, namely ‘f’ In the expression: lines f In my simplistic beginners way I think this is because when I run main = do … everything is or is within an ‘impure’ area but at ghci command line it is not and I’m mixing pure/impure functions. So my questions are 1. Is this simplistic view any where near correct? 2. How can I progress at the command line in ghci? Many thanks Mike

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

Thank you Friedrich, that really does help. At some point I became confused as I’m *sure* that I tried file <- readFile “poem.txt” in ghci and it didn’t work! Clearly it does. :) Thanks again. Mike
On 19 Apr 2015, at 11:03, Friedrich Wiemer
wrote: 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
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (2)
-
Friedrich Wiemer
-
Mike Houghton