Created
October 24, 2018 23:11
-
-
Save tmountain/694e270b99105e81eb137c5e6e41c9fd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Shape where | |
import Data.List (nub) | |
data Circle = Circle { radius :: Float } | |
data Triangle = Triangle { a :: Float, b :: Float, c :: Float } | |
class Dimensions shape where | |
name :: shape -> String | |
perimeter :: shape -> Float | |
area :: shape -> Float | |
triangleName :: Triangle -> String | |
triangleName t = | |
case length $ nub [a t, b t, c t] of | |
1 -> "equilateral" | |
2 -> "isoceles" | |
_ -> "scalene" | |
instance Dimensions Triangle where | |
name t = triangleName t | |
perimeter t = (a t) + (b t) + (c t) | |
area t = | |
let | |
a' = a t | |
b' = b t | |
c' = c t | |
s = (a' + b' + c') / 2 | |
in | |
sqrt(s * (s - a') * (s - b') * (s - c')) | |
instance Dimensions Circle where | |
name circle = "circle" | |
perimeter circle = 2 * pi * (radius circle) | |
area circle = pi * (radius circle)^2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment