Project Inquiry

Impelement Prelude.max and Prelude.min

Challenge
{-# LANGUAGE NoImplicitPrelude #-}

module Main where

import           Prelude hiding (max, min)

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

test :: Bool
test = and [ max 1 3 == 3
           , min 1 3 == 1
           , max 'a' 'c' == 'c'
           , min 'a' 'c' == 'a'
           ]

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

module Main where

import           Prelude hiding (max, min)

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

test :: Bool
test = and [ max 1 3 == 3
           , min 1 3 == 1
           , max 'a' 'c' == 'c'
           , min 'a' 'c' == 'a'
           ]

-- DONE
which :: (a -> a -> Bool) -> a -> a -> a
which f x y = if f x y then x else y

max :: (Ord a) => a -> a -> a
max = which (>)

min :: (Ord a) => a -> a -> a
min = which (<)