[Haskell-beginners] The type class Read

PY aquagnu at gmail.com
Thu Jul 12 11:08:29 UTC 2018


read "(3,'a')"::(Int, Char)  returns expected tuple. But with ":: Int" 
leads to runtime exception, because it calls `readsPrec` of Int and can 
not parse such string. But readsPrec of (a,b) where both are readable 
parses it successfully.


12.07.2018 13:05, mrx wrote:
> Hi,
>
> I'm trying to make sense of the type class `Read`. It feels to be like 
> a function performing casting to me, similar to what you have in C and 
> Java.
>
> The weird thing is that the casting doesn't have to make sense, 
> type-wise. So `read "5" :: Int` produce the integer 5 which make sense 
> but `read "(3,'a')" :: Int` doesn't make sense to me but ghci accepts it!
>
> This is seeing Read as a function, as it's a type class i suspect I'm 
> missing something. Quite possibly quite a lot...
>
> Why is the type class called `Read`?
> What am I missing above?
>
> // Patrik Iselind
>
>
> _______________________________________________
> 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/20180712/b55af5cf/attachment.html>


More information about the Beginners mailing list