praxis

Various programming exercises.
git clone git://git.jtobin.io/praxis.git
Log | Files | Refs

Overlap.hs (782B)


      1 {-# OPTIONS_GHC -Wall #-}
      2 
      3 data Range = Range !Int !Int
      4   deriving (Eq, Show)
      5 
      6 range :: Int -> Int -> Maybe Range
      7 range a b
      8   | a <= b    = Just (Range a b)
      9   | otherwise = Nothing
     10 
     11 overlap :: Range -> Range -> Maybe Range
     12 overlap (Range al bl) (Range ar br)
     13   | bl < ar || br < al = Nothing
     14   | otherwise          = range (max al ar) (min bl br)
     15 
     16 main :: IO ()
     17 main = do
     18   let bar = do
     19         r0 <- range 17 25
     20         r1 <- range 12 19
     21         overlap r0 r1
     22 
     23       baz = do
     24         r0 <- range 12 17
     25         r1 <- range 19 25
     26         overlap r0 r1
     27 
     28       qux = do
     29         r0 <- range 19 25
     30         r1 <- range 12 17
     31         overlap r0 r1
     32 
     33       zap = do
     34         r0 <- range 19 25
     35         r1 <- range 22 30
     36         overlap r0 r1
     37 
     38   print bar
     39   print baz
     40   print qux
     41   print zap
     42 
     43