[Haskell-beginners] Doubts regarding the "read" function.

Bob Ippolito bob at redivi.com
Sat Dec 13 08:03:19 UTC 2014


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 <venuchv at gmail.com>
wrote:

> 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20141213/cf2633d1/attachment.html>


More information about the Beginners mailing list