-- IB016 Seminar on Functional Programming -- Task from lecture 03 import Control.Applicative import Data.List ( sort ) newtype Triple a = Triple { unTriple :: (a, a, a) } deriving (Eq, Ord, Show, Read) instance Functor Triple where fmap f (Triple (x, y, z)) = Triple (f x, f y, f z) instance Applicative Triple where pure x = Triple (x, x, x) Triple (f, g, h) <*> Triple (x, y, z) = Triple (f x, g y, h z) expand :: Num a => a -> [a] expand x = [x-1, x, x+1] (~+~) :: Num a => a -> a -> [a] x ~+~ y = expand $ x + y (~*~) :: Num a => a -> a -> [a] x ~*~ y = liftA2 (*) (expand x) (pure y) ++ liftA2 (*) (pure x) (expand y) almostPlusTimes :: Num a => a -> a -> a -> [a] almostPlusTimes x y z = do tmp <- x ~+~ y tmp2 <- tmp ~*~ z return tmp2 maxMinEval :: (Num a, Ord a) => [a] -> (a, a) maxMinEval list = (head sorted, last sorted) where sorted = sort list