Project Inquiry

Simplistic Email Validation

Challenge
{-# LANGUAGE NoImplicitPrelude #-}

module Main where

import           Prelude

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

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

test :: Bool
test = and [ makeEmail "tony@stark.com" == Just (Email "tony@stark.com")
           , makeEmail "tony" == Nothing
           , makeEmail "Tony" == Nothing
           , makeEmail "Tony (at) Stark.com" == Nothing
           -- Keep it simple ...
           , makeEmail "@" == Just (Email "@")
           , makeEmail "Iron@" == Just (Email "Iron@")
           , makeEmail "@Man" == Just (Email "@Man")
           ]

-- TODO: This program doesn't compile. Define the missing function(s) incl. type signatures!
-- TIP:  You can consider all Strings that contain an '@' to be valid email addresses.
Solution
{-# LANGUAGE NoImplicitPrelude #-}

module Main where

import           Prelude

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

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

test :: Bool
test = and [ makeEmail "tony@stark.com" == Just (Email "tony@stark.com")
           , makeEmail "tony" == Nothing
           , makeEmail "Tony" == Nothing
           , makeEmail "Tony (at) Stark.com" == Nothing
           -- Keep it simple ...
           , makeEmail "@" == Just (Email "@")
           , makeEmail "Iron@" == Just (Email "Iron@")
           , makeEmail "@Man" == Just (Email "@Man")
           ]

-- DONE
makeEmail :: String -> Maybe Email
makeEmail s = if ('@' `elem` s) then Just (Email s) else Nothing