Advertisement
ttaaa

PointReader

Jan 17th, 2022
667
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module PointReader
  2.     (
  3.         readPoint,
  4.         isEnd,
  5.         isError,
  6.         getPoint
  7.     ) where
  8.  
  9. import Point
  10. import Data.Text as Text
  11.  
  12. data ReadedPoint = Point Double Double | END_OF_READING | READING_ERROR deriving (Eq, Show)
  13.  
  14. readPoint :: IO (ReadedPoint)
  15. readPoint = do
  16.     line <- getLine
  17.     if (line == "END") then
  18.         return END_OF_READING
  19.     else
  20.         return (parseString (Prelude.filter (\c -> c /= ' ') line))
  21.  
  22. parseString :: String -> ReadedPoint
  23. parseString line =
  24.     let ds = Prelude.map Text.unpack (Text.splitOn (Text.pack ";") (Text.pack line))
  25.     in if (Prelude.length ds /= 2) then
  26.         READING_ERROR
  27.     else let d1 = reads (ds !! 0) :: [(Double, String)]
  28.              d2 = reads (ds !! 1) :: [(Double, String)]
  29.          in if (Prelude.null d1 || Prelude.null d2) then
  30.             READING_ERROR
  31.         else
  32.             let (x, t1) = d1 !! 0
  33.                 (y, t2) = d2 !! 0
  34.             in if (not (Prelude.null t1) || not (Prelude.null t2)) then
  35.                 READING_ERROR
  36.             else
  37.                 Point x y
  38.  
  39. isEnd :: ReadedPoint -> Bool
  40. isEnd rp = rp == END_OF_READING
  41.  
  42. isError :: ReadedPoint -> Bool
  43. isError rp = rp == READING_ERROR
  44.  
  45. getPoint :: ReadedPoint -> Point
  46. getPoint (Point x y) = createPoint x y
  47. getPoint rp = error ("Can't be converted to Point: " ++ show rp)
  48.  
  49.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement