Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Doubts regarding the "read" function. (Venu Chakravorty)
   2. Re:  Doubts regarding the "read" function. (Bob Ippolito)


----------------------------------------------------------------------

Message: 1
Date: Sat, 13 Dec 2014 13:14:07 +0530
From: Venu Chakravorty <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] Doubts regarding the "read" function.
Message-ID:
        <CAH9fVMN2SRF12anr9D9qMtu=wrfhb1gwokn+xhtzevqgdrd...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

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.


------------------------------

Message: 2
Date: Sat, 13 Dec 2014 00:03:19 -0800
From: Bob Ippolito <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Doubts regarding the "read" function.
Message-ID:
        <cacwmpm9rzzwzjjuv3x1nudxg9wha4m2mf-5rttdd+2-kq-k...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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 <[email protected]>
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
> [email protected]
> 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-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 78, Issue 7
****************************************

Reply via email to