Project Inquiry

Shapes area

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 [ area (Square 10.0) == 100.0
           , area (Rectangle 10.0 5.0) == 50.0
           , area (Circle 10.0) == 100.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 [ area (Square 10.0) == 100.0
           , area (Rectangle 10.0 5.0) == 50.0
           , area (Circle 10.0) == 100.0 * pi
           ]

-- DONE
area :: Shape -> Double
area (Square a)      = a ^ 2
area (Rectangle a b) = a * b
area (Circle r)      = r ^ 2 * pi