Skip to content

Instantly share code, notes, and snippets.

@gilesbradshaw
Created October 29, 2015 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilesbradshaw/151be287d705564652e4 to your computer and use it in GitHub Desktop.
Save gilesbradshaw/151be287d705564652e4 to your computer and use it in GitHub Desktop.
module Shapes
( Point(..)
, Shape(..)
, area
, nudge
, baseCircle
, baseRectangle
) where
data Point = Point Float Float deriving (Show)
data Shape = Circle Point Float | Rectangle Point Point
deriving (Show)
area :: Shape -> Float
area (Circle _ r) = pi * r ^2
area (Rectangle (Point x1 y1) (Point x2 y2)) = (abs $ x2 - x1) * (abs $ y2 - y1)
nudge :: Shape -> Float -> Float -> Shape
nudge (Circle (Point x y) r) a b = Circle (Point (x + a) (y + b)) r
nudge (Rectangle (Point x1 y1) (Point x2 y2)) a b = Rectangle (Point (x1 + a) (y1 + a)) (Point (x2 + a) (y2 + a))
baseCircle :: Float -> Shape
baseCircle r = Circle (Point 0 0) r
baseRectangle :: Float -> Float -> Shape
baseRectangle width height = (Rectangle (Point 0 0) (Point width height))
Enter file contents here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment