Project Inquiry

Case-Insensitive Email Comparison, Vol. 2

Challenge
{-# LANGUAGE NoImplicitPrelude #-}

module Main where

import           Prelude
import           Data.Char (toLower)

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

data Email = Email { name :: String, domain :: String }

test :: Bool
test = and [ Email "tony" "stark.com" == Email "tony" "stark.com"
           , Email "Tony" "Stark.com" == Email "tony" "stark.com"
           , Email "Tony" "ironman.com" /= Email "tonn" "stark.com"
           ]

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

module Main where

import           Prelude
import           Data.Char (toLower)

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

data Email = Email { name :: String, domain :: String }

test :: Bool
test = and [ Email "tony" "stark.com" == Email "tony" "stark.com"
           , Email "Tony" "Stark.com" == Email "tony" "stark.com"
           , Email "Tony" "ironman.com" /= Email "tonn" "stark.com"
           ]

-- DONE
instance Eq Email where
  (==) a b = let iCompare a b = fmap toLower a == fmap toLower b
             in iCompare (name a) (name b) && iCompare (domain a) (domain b)