Doubts regarding the "read" function.

Hello everyone, I am new to Haskell and this might seem very naive, please bear with me. ======================================= Prelude> read "4" + 4 8 Prelude> (read "4" :: Int) + 4 8 Prelude> read "hello " ++ "world" "*** Exception: Prelude.read: no parse Prelude> (read "hello" :: String) ++ " world" "*** Exception: Prelude.read: no parse ======================================= Could someone please explain why the last two statements don't work? My understanding is that "read" has a type of "read :: (Read a) => String -> a". So, "read "hello" " should give me an instance of type "Read" to which I am appending the string "world" (just like the first 2 cases where I get an instance of "Read" ("Int" in this case) to which I am adding another "Int" and I get a "Num" which is then displayed). I expected to see "hello world" as the output. Is it that the type "String" is not an instance of type class "Read"? Please tell me what I am missing here. Regards, Venu Chakravorty.

Read instances tend to be implemented such that they parse a string that
looks like the source code for that type. This is usually the inverse of
Show.
λ> show "hello"
"\"hello\""
λ> read "\"hello\"" :: String
"hello"
λ> read (show "hello") :: String
"hello"
On Fri, Dec 12, 2014 at 11:44 PM, Venu Chakravorty
Hello everyone,
I am new to Haskell and this might seem very naive, please bear with me.
======================================= Prelude> read "4" + 4 8 Prelude> (read "4" :: Int) + 4 8 Prelude> read "hello " ++ "world" "*** Exception: Prelude.read: no parse Prelude> (read "hello" :: String) ++ " world" "*** Exception: Prelude.read: no parse =======================================
Could someone please explain why the last two statements don't work?
My understanding is that "read" has a type of "read :: (Read a) => String -> a". So, "read "hello" " should give me an instance of type "Read" to which I am appending the string "world" (just like the first 2 cases where I get an instance of "Read" ("Int" in this case) to which I am adding another "Int" and I get a "Num" which is then displayed). I expected to see "hello world" as the output.
Is it that the type "String" is not an instance of type class "Read"? Please tell me what I am missing here.
Regards, Venu Chakravorty. _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (3)
-
Bob Ippolito
-
Kim-Ee Yeoh
-
Venu Chakravorty