Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. import Text.ParserCombinators.ReadP
  2.  
  3. isNumericOrSep :: Char -> Bool
  4. isNumericOrSep = flip elem $ "0123456789-, "
  5.  
  6. tuplify2 :: [Int] -> (Int, Int)
  7. tuplify2 [x,y] = (x,y)
  8.  
  9. tupleAsIntPair :: ReadP (Int, Int)
  10. tupleAsIntPair = fmap tuplify2 parsedList
  11. where parsedList = fmap (map read) $ sepBy1 noparens sep
  12. noparens = between open close $ many1 (satisfy isNumericOrSep)
  13. open = char '('
  14. close = char ')'
  15. sep = char ','
  16.  
  17. isNumericOrSep :: Char -> Bool
  18. isNumericOrSep = flip elem $ "0123456789- "
  19.  
  20. tupleAsIntPair :: ReadP (Int,Int)
  21. tupleAsIntPair = fmap tuplify2 parsedList
  22. where
  23. parsedList = fmap (map read) $ between open close $ sepBy1 noparens sep
  24. noparens = many1 (satisfy isNumericOrSep)
  25. open = char '('
  26. close = char ')'
  27. sep = char ','
  28.  
  29. *Main> (readP_to_S tupleAsIntPair) "(3,4)"
  30. [((3,4),"")]
  31.  
  32. between :: ReadP open -> ReadP close -> ReadP a -> ReadP a
  33. -- ^ @between open close p@ parses @open@, followed by @p@ and finally
  34. -- @close@. Only the value of @p@ is returned.
  35. between open close p = do _ <- open
  36. x <- p
  37. _ <- close
  38. return x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement