Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Text.ParserCombinators.ReadP
- isNumericOrSep :: Char -> Bool
- isNumericOrSep = flip elem $ "0123456789-, "
- tuplify2 :: [Int] -> (Int, Int)
- tuplify2 [x,y] = (x,y)
- tupleAsIntPair :: ReadP (Int, Int)
- tupleAsIntPair = fmap tuplify2 parsedList
- where parsedList = fmap (map read) $ sepBy1 noparens sep
- noparens = between open close $ many1 (satisfy isNumericOrSep)
- open = char '('
- close = char ')'
- sep = char ','
- isNumericOrSep :: Char -> Bool
- isNumericOrSep = flip elem $ "0123456789- "
- tupleAsIntPair :: ReadP (Int,Int)
- tupleAsIntPair = fmap tuplify2 parsedList
- where
- parsedList = fmap (map read) $ between open close $ sepBy1 noparens sep
- noparens = many1 (satisfy isNumericOrSep)
- open = char '('
- close = char ')'
- sep = char ','
- *Main> (readP_to_S tupleAsIntPair) "(3,4)"
- [((3,4),"")]
- between :: ReadP open -> ReadP close -> ReadP a -> ReadP a
- -- ^ @between open close p@ parses @open@, followed by @p@ and finally
- -- @close@. Only the value of @p@ is returned.
- between open close p = do _ <- open
- x <- p
- _ <- close
- return x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement