import Text.Parsec
import Text.Parsec.String
import Text.Parsec.Prim
import Text.Parsec.Char
data List = List [Float] deriving Show
let -- Parse a nonempty int list like [1, 2, 3]
parser :: Parser List
parser = do
char '['
values <- option [] $ many1 (try float <|> int)
char ']'
return $ List values
-- Parse an element of an int list, like "3, "
int :: Parser Float
int = do
value <- many1 $ oneOf "0123456789"
optional $ char ','
whitespace
return (fromIntegral (read value :: Int) :: Float)
float :: Parser Float
float = do
value <- many1 $ oneOf "0123456789"
char '.'
after <- many1 $ oneOf "0123456789"
optional $ char ','
whitespace
return (read (value ++ "." ++ after) :: Float)
-- Parse any whitespace
whitespace = many $ oneOf " \t"