[Haskell-beginners] The type class Read
mrx
patrik.mrx at gmail.com
Fri Jul 13 08:14:56 UTC 2018
Ah, I see. Thanks a lot for the clarification!
Patrik Iselind
Den fre 13 juli 2018 09:31Francesco Ariis <fa-ml at 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 at 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.html>
More information about the Beginners
mailing list