Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module PointReader
- (
- readPoint,
- isEnd,
- isError,
- getPoint
- ) where
- import Point
- import Data.Text as Text
- data ReadedPoint = Point Double Double | END_OF_READING | READING_ERROR deriving (Eq, Show)
- readPoint :: IO (ReadedPoint)
- readPoint = do
- line <- getLine
- if (line == "END") then
- return END_OF_READING
- else
- return (parseString (Prelude.filter (\c -> c /= ' ') line))
- parseString :: String -> ReadedPoint
- parseString line =
- let ds = Prelude.map Text.unpack (Text.splitOn (Text.pack ";") (Text.pack line))
- in if (Prelude.length ds /= 2) then
- READING_ERROR
- else let d1 = reads (ds !! 0) :: [(Double, String)]
- d2 = reads (ds !! 1) :: [(Double, String)]
- in if (Prelude.null d1 || Prelude.null d2) then
- READING_ERROR
- else
- let (x, t1) = d1 !! 0
- (y, t2) = d2 !! 0
- in if (not (Prelude.null t1) || not (Prelude.null t2)) then
- READING_ERROR
- else
- Point x y
- isEnd :: ReadedPoint -> Bool
- isEnd rp = rp == END_OF_READING
- isError :: ReadedPoint -> Bool
- isError rp = rp == READING_ERROR
- getPoint :: ReadedPoint -> Point
- getPoint (Point x y) = createPoint x y
- getPoint rp = error ("Can't be converted to Point: " ++ show rp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement