Skip to content

Instantly share code, notes, and snippets.

@dewey92
Created May 2, 2019 18:23
Show Gist options
  • Select an option

  • Save dewey92/be6a66a5ee81f10ed8fcd0eaa99a76d4 to your computer and use it in GitHub Desktop.

Select an option

Save dewey92/be6a66a5ee81f10ed8fcd0eaa99a76d4 to your computer and use it in GitHub Desktop.
Arbitrary ADT
-- Data
data Shape
= Rectangle Number Number -- length, height
| Triangle Number Number -- base, height
| Circle Number -- radius
| Square Number -- length
-- Behaviour
area :: Shape -> Number
area (Rectangle l h) = l * h
area (Triangle b h) = b * h / 2
area (Circle r) = 3.14 * r * r
area (Square l) = l * l
-- Example
area (Rectangle 5 4) == 20
area (Circle 7) == 154
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment