Project Inquiry

Shapes circumference

Challenge
{-# LANGUAGE NoImplicitPrelude #-}

module Main where

import           Prelude

main :: IO ()
main = putStrLn $ if test then "SUCCESS" else "FAILURE"

data Shape = Square Double
           | Rectangle Double Double
           | Circle Double -- e.g., radius

test :: Bool
test = and [ circumference (Square 10.0) == 40.0
           , circumference (Rectangle 10.0 5.0) == 30.0
           , circumference (Circle 10.0) == 20.0 * pi
           ]

-- TODO: This program doesn't compile. Define the missing function(s) incl. type signatures!
Solution
{-# LANGUAGE NoImplicitPrelude #-}

module Main where

import           Prelude

main :: IO ()
main = putStrLn $ if test then "SUCCESS" else "FAILURE"

data Shape = Square Double
           | Rectangle Double Double
           | Circle Double -- e.g., radius

test :: Bool
test = and [ circumference (Square 10.0) == 40.0
           , circumference (Rectangle 10.0 5.0) == 30.0
           , circumference (Circle 10.0) == 20.0 * pi
           ]

-- DONE
circumference :: Shape -> Double
circumference (Square a)      = 4 * a
circumference (Rectangle a b) = 2 * a + 2 * b
circumference (Circle r)      = 2 * r * pi