Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE ForeignFunctionInterface #-}
  2.  
  3. import Foreign
  4. import Foreign.C.Types
  5.  
  6. data Point =
  7.   Point Float Float deriving (Show)
  8.  
  9. data Circle =
  10.   Circle Point Float deriving (Show)
  11.  
  12. distance :: Point -> Point -> Float
  13. distance (Point x1 y1) (Point x2 y2) =
  14.   sqrt $ (abs $ x1 - x2) + (abs $ y1 - y2)
  15.  
  16. intersect :: Circle -> Circle -> Bool
  17. intersect (Circle p1 r1) (Circle p2 r2) =
  18.   distance p1 p2 <= r1 + r2
  19.  
  20. area :: Circle -> Float
  21. area (Circle _ r) = r * r * 2 * pi
  22.  
  23. data Option
  24.   = PosixSyntax
  25.   | LongestMatch
  26.   | LogError
  27.   | Literal
  28.   | NeverNl
  29.   | DotNl
  30.   | NeverCapture
  31.   | CaseSensitive
  32.   | PerlCases
  33.   | WordBoundary
  34.   | OneLine
  35.   deriving (Show)
  36.  
  37. data Re = Re
  38.   { regex :: [Char]
  39.   , options :: [(Option, Bool)]
  40.   } deriving (Show)
  41.  
  42. defaultOptions =
  43.   [ (PosixSyntax, False)
  44.   , (LongestMatch, False)
  45.   , (LogError, False)
  46.   , (Literal, False)
  47.   , (NeverNl, False)
  48.   , (DotNl, False)
  49.   , (NeverCapture, False)
  50.   , (CaseSensitive, False)
  51.   , (PerlCases, False)
  52.   , (WordBoundary, False)
  53.   , (OneLine, False)
  54.   ]
  55.  
  56. makeRe :: [Char] -> Re
  57. makeRe x = Re { regex = x,  options = defaultOptions }
  58.  
  59. makeRe' :: [Char] -> [(Option, Bool)] -> Re
  60. makeRe' x o = Re { regex = x, options = o }
  61.  
  62. main :: IO()
  63. main = do
  64.   putStrLn $ show $ distance (Point 0 0) (Point 1 1)
  65.   putStrLn $ show $ (Point 10 5)
  66.   putStrLn $ show $
  67.     intersect (Circle (Point 1 1) 1)
  68.               (Circle (Point 5 5) 1)
  69.   putStrLn $ show $ area (Circle (Point 0 0) 5)
  70.   putStrLn $ show $ makeRe "Hello, World!"
  71.   putStrLn $ show $ makeRe' "Hello, World" []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement