praxis

Various programming exercises.
Log | Files | Refs

commit 93c1c0aa179fb8e13fcbe100a75168acc1c48fbf
parent 82d5dd058a01747770d46c8f38b083ef6d8f8695
Author: Jared Tobin <jared@jtobin.ca>
Date:   Tue, 26 Jun 2018 10:23:39 +1200

Overlapping thing.

Diffstat:
A20180608_overlap/Overlap.hs | 43+++++++++++++++++++++++++++++++++++++++++++
1 file changed, 43 insertions(+), 0 deletions(-)

diff --git a/20180608_overlap/Overlap.hs b/20180608_overlap/Overlap.hs @@ -0,0 +1,43 @@ +{-# OPTIONS_GHC -Wall #-} + +data Range = Range !Int !Int + deriving (Eq, Show) + +range :: Int -> Int -> Maybe Range +range a b + | a <= b = Just (Range a b) + | otherwise = Nothing + +overlap :: Range -> Range -> Maybe Range +overlap (Range al bl) (Range ar br) + | bl < ar || br < al = Nothing + | otherwise = range (max al ar) (min bl br) + +main :: IO () +main = do + let bar = do + r0 <- range 17 25 + r1 <- range 12 19 + overlap r0 r1 + + baz = do + r0 <- range 12 17 + r1 <- range 19 25 + overlap r0 r1 + + qux = do + r0 <- range 19 25 + r1 <- range 12 17 + overlap r0 r1 + + zap = do + r0 <- range 19 25 + r1 <- range 22 30 + overlap r0 r1 + + print bar + print baz + print qux + print zap + +