I'm trying to parse a string in haskell using Parsec.Combinator library. But I can't find how to parse a floating value. My function only reads integer (with digit, but digit only parses a integer value).
parsePoint :: Parser Point
parsePoint = do
string "Point"
x <- many1 digit
char ','
y <- many1 digit
return $ (Point (read x) (read y))
I searched for Text.Parsec.Number library but didn't find examples to how to use.
thanks for reading
There are several Parser
s that can parse floats, for example floating :: (Floating f, Stream s m Char) => ParsecT s u m f
. The Parser
is just an alias for a special case of Parsec
, depending on which module you use. For example type Parser = Parsec Text ()
.
If your Point
thus accepts for example two Float
s, you can use the parsec3-numbers
package and work with:
import Text.Parsec.Char(char, string)
import Text.Parsec.Number(floating)
import Text.Parsec.Text(Parser)
data Point = Point Float Float
parsePoint :: Parser Point
parsePoint = do
string "Point "
x <- floating
char ','
y <- floating
return (Point x y)
This then can parse, in this case a string "Point 3.14,2.718"