Project Inquiry

List maximum and minimum

Challenge
{-# LANGUAGE NoImplicitPrelude #-}

module Main where

import           Prelude hiding (maximum, minimum)
import           Data.Maybe (fromMaybe)

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

test :: Bool
test = and [ maximum ([] :: [Int]) == Nothing
           , minimum ([] :: [Int]) == Nothing
           , maximum [1, 3, 2] == Just 3
           , minimum [2, 1, 3] == Just 1
           , maximum "abc" == Just 'c'
           , minimum "abc" == Just 'a'
           ]

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

module Main where

import           Prelude hiding (maximum, minimum)
import           Data.Maybe (fromMaybe)

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

test :: Bool
test = and [ maximum ([] :: [Int]) == Nothing
           , minimum ([] :: [Int]) == Nothing
           , maximum [1, 3, 2] == Just 3
           , minimum [2, 1, 3] == Just 1
           , maximum "abc" == Just 'c'
           , minimum "abc" == Just 'a'
           ]

-- DONE
maximum :: (Ord a) => [a] -> Maybe a
maximum []     = Nothing
maximum (x:xs) = Just $ max x $ fromMaybe x (maximum xs)

minimum :: (Ord a) => [a] -> Maybe a
minimum []     = Nothing
minimum (x:xs) = Just $ min x $ fromMaybe x (minimum xs)