In another question, one of the comments says, "[Data.]Text is becoming the de-facto textual implementation. String is still around for legacy reasons and for simple things, but for serious textual manipulation you should be using Text."
What is the easiest way to convert a Data.Text to an Int? read will not work because the read function always takes a String.
The best that I can come up with is:
let fortyTwo = Data.Text.pack "42" read $ Data.Text.unpack fortyTwo :: Int Is there a better way?
int i = int. Parse(s);
The unary + operator converts its argument to a double precision floating point. String floatString = "14.5"; float x = Float. parseFloat(floatString); double y = Double. parseFloat(floatString);
In Haskell a String is just a list of Char s, indeed type String = [Char] . String is just an "alias" for such list. So all functions you define on lists work on strings, given the elements of that list are Char s.
Text is a more efficient alternative to Haskell's standard String type. String is defined as a linked list of characters in the standard Prelude, per the Haskell Report: type String = [Char]
Looking at the text package, I see a module called Data.Text.Read. It seems to work:
λ> decimal (T.pack "99 bottles") Right (99," bottles") λ> decimal (T.pack "a digit") Left "input does not start with a digit"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With