
Hello, I have a small piece of code that does not compile and I'm having trouble figuring out why. Here is the relevant snippet: readChunks :: Handle -> [String] -> IO [String] readChunks handle accum = do chunk <- readHeaderChunk handle if isLast chunk then return (accum ++ chunk) else return (readChunks handle (accum ++ chunk)) isLast :: [String] -> Bool readHeaderChunk :: Handle -> IO [String] And here is the single compiler error: hacks.hs:48:18: Couldn't match expected type `[String]' with actual type `IO [String]' In the return type of a call of `readChunks' In the first argument of `return', namely `(readChunks handle (accum ++ chunk))' In the expression: return (readChunks handle (accum ++ chunk)) What I'm most confused about is that chunk is passed to isLast as a [String] with no compiler error but cannot be passed to accum ++ chunk that way. Or so it seems. Can someone she some light on this? Thanks.

readChunks is already in the IO context, so return (which, confusingly, is
not a keyword to return a value from a function, but a function that puts a
value back in a context) in the last line of readChunks needs to be deleted.
On Mon, May 25, 2015 at 12:42 PM, Barry DeZonia
Hello,
I have a small piece of code that does not compile and I'm having trouble figuring out why.
Here is the relevant snippet:
readChunks :: Handle -> [String] -> IO [String] readChunks handle accum = do chunk <- readHeaderChunk handle if isLast chunk then return (accum ++ chunk) else return (readChunks handle (accum ++ chunk))
isLast :: [String] -> Bool
readHeaderChunk :: Handle -> IO [String]
And here is the single compiler error:
hacks.hs:48:18: Couldn't match expected type `[String]' with actual type `IO [String]' In the return type of a call of `readChunks' In the first argument of `return', namely `(readChunks handle (accum ++ chunk))' In the expression: return (readChunks handle (accum ++ chunk))
What I'm most confused about is that chunk is passed to isLast as a [String] with no compiler error but cannot be passed to accum ++ chunk that way. Or so it seems. Can someone she some light on this? Thanks.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

On 25 May 2015 at 23:12, Barry DeZonia
Hello,
I have a small piece of code that does not compile and I'm having trouble figuring out why.
Here is the relevant snippet:
readChunks :: Handle -> [String] -> IO [String] readChunks handle accum = do chunk <- readHeaderChunk handle if isLast chunk then return (accum ++ chunk) else return (readChunks handle (accum ++ chunk))
In short, try: else readChunks handle (accum ++ chunk) Explanation, return :: a -> IO a (in this case) readChunks handle (accum ++ chunk) :: IO [String] therefore, return (readChunks handle (accum ++ chunk)) :: IO (IO [String])
isLast :: [String] -> Bool
readHeaderChunk :: Handle -> IO [String]
And here is the single compiler error:
hacks.hs:48:18: Couldn't match expected type `[String]' with actual type `IO [String]'
Wanted [String], got IO [String] In the return type of a call of `readChunks'
In the first argument of `return', namely `(readChunks handle (accum ++ chunk))' In the expression: return (readChunks handle (accum ++ chunk))
What I'm most confused about is that chunk is passed to isLast as a [String] with no compiler error but cannot be passed to accum ++ chunk that way. Or so it seems. Can someone she some light on this? Thanks.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-- Regards Sumit Sahrawat
participants (3)
-
Barry DeZonia
-
Mike Meyer
-
Sumit Sahrawat, Maths & Computing, IIT (BHU)