Skip to content

Instantly share code, notes, and snippets.

@tmountain
Created October 24, 2018 23:11
Show Gist options
  • Save tmountain/694e270b99105e81eb137c5e6e41c9fd to your computer and use it in GitHub Desktop.
Save tmountain/694e270b99105e81eb137c5e6e41c9fd to your computer and use it in GitHub Desktop.
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