[Haskell-beginners] Data.Text to Int?

Emmanuel Touzery etouzery at gmail.com
Fri Dec 14 16:19:02 CET 2012


>
> An instance of Read means that read can *produce* a Text, not that it 
> can consume one.  read always reads from a String, as you can see from 
> its type:
>
>     Prelude Data.Text> :t read
>     read :: Read a => String -> a
>
> (Note that it is the result type a in the context for Read.)

yes, seeing the type signature I understood it. I was googling it 
without much luck, I didn't think of using ghci. I'll use that next time.

> See the Data.Text.Read module (part of the text package you already 
> have installed) for how to do similar things with a Text as a source.
>

I see... However it uses Either and returns a pair, unlike "read". It's 
a plus for reliability but an annoyance in my case. In my case I know 
positively it's a number.
In this case I did a filter isDigit, but this will happen also if I 
match using a regular expression and [0-9] or \d.

In the end the most terse way to code it is to go through unpack then it 
seems.
Using Data.Text.Read all I see is:

fst $ right $ decimal t
where right (Right a) = a

so I'll probably do:

read $ unpack t

and be done with it...

Thank you!

emmanuel



More information about the Beginners mailing list