You are viewing sancho_ex

overlap function in Haskell for Retangles

November 20th, 2006 (12:01 pm)

I am continuing with Haskell despite the Scala language vieing for my attention. This is my partial solution to Exercise 14.11 from "Haskell: The Craft of Functional Programming" 2/e. It includes the test data.

-- Exercise 14.11
-- Define a function to test whether two NewShapes overlap
overlap :: NewShape -> NewShape -> Bool
overlap (NewRectangle (Point x y) h w) (NewRectangle (Point x' y') h' w')
  | x + w <= x' = False
  | x >= x' + w' = False
  | y + h <= y' = False
  | y == y' + h' = False
  | otherwise = True
  
r1 = NewRectangle (Point 0 0) 1 1 
r2 = NewRectangle (Point 1 0) 1 1

r3 = NewRectangle (Point 0 1) 1 1
r4 = NewRectangle (Point 0.5 0.6) 1 1