-- Comp1100
-- Sem 1 2006

-- Shapes: an Algebraic Data Type Example

-- Clem Baker-Finch March 2006

module Shape where

-- The argument to the Circle constructor is the radius
-- The arguments to the Rectangle constructor are width and height
-- Ellipse: semi-major and semi-minor axes
-- Square: side
-- Add some more.  Go nuts.
data Shape = Circle Float | Rectangle Float Float
	   | Ellipse Float Float | Square Float
             deriving (Eq, Show)

-- Notice pattern matching form of definition
isRound :: Shape -> Bool
isRound (Circle rad)      = True
isRound (Ellipse maj min) = True
isRound other             = False

-- Calculate the area of a shape
area :: Shape -> Float
area (Circle rad)      = pi * rad^2
area (Rectangle l b)   = l * b
area (Ellipse maj min) = pi * maj * min
area (Square side)     = side^2

-- Do some more: perimeter for a start.

-- Normalise: call a rectangle with equal sides a square
-- Ditto an ellipse with equal axes
normalise :: Shape -> Shape
normalise (Rectangle l b)
    | l == b     = Square l
    | otherwise  = Rectangle l b
normalise (Ellipse maj min)
    | maj == min = Circle maj
    | otherwise  = Ellipse maj min
normalise other  = other


