[Haskell-beginners] Data.Text to Int?

Emmanuel Touzery etouzery at gmail.com
Fri Dec 14 16:03:38 CET 2012


I see I got it wrong...
It's the other way around in read x :: Int, it's Int which has an 
instance of Read...

http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:read

So if Data.Text has an instance of Read I can convert a String to 
Data.Text using Read...

Not what I wanted.

Must I really do Data.Text->String->Int or is there a direct way?

emmanuel

On 14.12.2012 15:57, Emmanuel Touzery wrote:
> Hello,
>
>     I expected "read" to work directly on a Data.Text because 
> Data.Text has an instance of the Read class, but it seems to fail:
>
> ---------
> import Data.Text
>
> main = do
>     let a = pack "1"
>     let b = read a :: Int
>     putStrLn "parsed OK"
> ---------
>
> test.hs:5:22:
>     Couldn't match expected type `String' with actual type `Text'
>     In the first argument of `read', namely `a'
>     In the expression: read a :: Int
>     In an equation for `b': b = read a :: Int
>
>
> Sure I can do"read (unpack a) :: Int" but there must be a way to do it 
> directly?
>
> Emmanuel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121214/b91a7fce/attachment.htm>


More information about the Beginners mailing list