Project Inquiry

Implement Ord type-class

Challenge
{-# LANGUAGE NoImplicitPrelude #-}

module Main where

import           Prelude
import           Data.List (sort)

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

newtype Email = Email { unEmail :: String } deriving (Eq)

test :: Bool
test = let a = Email "a@example.com"
           b = Email "b@example.com"
           c = Email "c@example.com" in
       and [ sort ([] :: [Email]) == []
           , sort [a, b] == [a, b]
           , sort [a, b, c] == [a, b, c]
           , sort [b, a, c] == [a, b, c]
           , sort [a, c, b] == [a, b, c]
           ]

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

module Main where

import           Prelude
import           Data.List (sort)

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

newtype Email = Email { unEmail :: String } deriving (Eq)

test :: Bool
test = let a = Email "a@example.com"
           b = Email "b@example.com"
           c = Email "c@example.com" in
       and [ sort ([] :: [Email]) == []
           , sort [a, b] == [a, b]
           , sort [a, b, c] == [a, b, c]
           , sort [b, a, c] == [a, b, c]
           , sort [a, c, b] == [a, b, c]
           ]

-- DONE
instance Ord Email where
  compare a b = unEmail a `compare` unEmail b