Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

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


Today's Topics:

   1. Re:  The type class Read (mrx)
   2. Re:  The type class Read (Francesco Ariis)
   3. Re:  The type class Read (mrx)


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

Message: 1
Date: Fri, 13 Jul 2018 09:06:51 +0200
From: mrx <patrik....@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] The type class Read
Message-ID:
        <canzojbjv-rnbywo3m4uucv1tkfagbq5d35+kprl7pm0tpyf...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Den tors 12 juli 2018 13:15Francesco Ariis <fa...@ariis.it> skrev:

> Hello Patrik,
>
> On Thu, Jul 12, 2018 at 12:05:52PM +0200, mrx wrote:
> > Why is the type class called `Read`?
> > What am I missing above?
>
> we can say any instance of `Read` has to implement
>
>     read :: Read a => String -> a
>     -- the actual instance implements a different function,
>     -- but that's not relevant for our example
>
> So, a typeclass (Read, capital `r`) gives us a function (`read`,
> lower-case `r`).  The function goes from  `String` (and no other
> things) to our implemented type.
>

That makes sense to me based on the type, sure. So read is some form of
casting then?

Does this answers your question?


Maybe, but I still don't see what I'd use it for. Is it used to for example
read the contents of a file whose file name is provided as that string?

// Patrik
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20180713/fe4f9145/attachment-0001.html>

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

Message: 2
Date: Fri, 13 Jul 2018 09:30:37 +0200
From: Francesco Ariis <fa...@ariis.it>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] The type class Read
Message-ID: <20180713073037.4edxksgtcmb4r...@x60s.casa>
Content-Type: text/plain; charset=utf-8

On Fri, Jul 13, 2018 at 09:06:51AM +0200, mrx wrote:
> That makes sense to me based on the type, sure. So read is some form of
> casting then?

Yep, but just from `String` and nothing else.

> 
> Does this answers your question?
> 
> 
> Maybe, but I still don't see what I'd use it for. Is it used to for example
> read the contents of a file whose file name is provided as that string?

No, you would use `readFile` for that:

    readFile :: FilePath -> IO String
    -- Filepath is a type synonym for `String`

You would use `read` to convert simple user input (which is usually
collected as String) into, say, Integers

    getLine :: IO String
    -- this could need read

And in general, `Read` is supposed to be compatible with `Show`, so
if you used `show` for any reason (some form of cheap serialisation,
etc.), `read` should work back the type:

    λ> show [1..10]
    "[1,2,3,4,5,6,7,8,9,10]"
    λ> read it :: [Int]
    [1,2,3,4,5,6,7,8,9,10]

tl;dr: cheap type parsing. For any more specialised/complex parsing,
use a proper parsing library like Parsec.


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

Message: 3
Date: Fri, 13 Jul 2018 10:14:56 +0200
From: mrx <patrik....@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] The type class Read
Message-ID:
        <CANzOjBh8GnpYA-EGPq2oEXyTZYRQDL=uquqtkqmzxbtamec...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Ah, I see. Thanks a lot for the clarification!

Patrik Iselind

Den fre 13 juli 2018 09:31Francesco Ariis <fa...@ariis.it> skrev:

> On Fri, Jul 13, 2018 at 09:06:51AM +0200, mrx wrote:
> > That makes sense to me based on the type, sure. So read is some form of
> > casting then?
>
> Yep, but just from `String` and nothing else.
>
> >
> > Does this answers your question?
> >
> >
> > Maybe, but I still don't see what I'd use it for. Is it used to for
> example
> > read the contents of a file whose file name is provided as that string?
>
> No, you would use `readFile` for that:
>
>     readFile :: FilePath -> IO String
>     -- Filepath is a type synonym for `String`
>
> You would use `read` to convert simple user input (which is usually
> collected as String) into, say, Integers
>
>     getLine :: IO String
>     -- this could need read
>
> And in general, `Read` is supposed to be compatible with `Show`, so
> if you used `show` for any reason (some form of cheap serialisation,
> etc.), `read` should work back the type:
>
>     λ> show [1..10]
>     "[1,2,3,4,5,6,7,8,9,10]"
>     λ> read it :: [Int]
>     [1,2,3,4,5,6,7,8,9,10]
>
> tl;dr: cheap type parsing. For any more specialised/complex parsing,
> use a proper parsing library like Parsec.
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20180713/d14eb174/attachment-0001.html>

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 121, Issue 14
******************************************

Reply via email to