-- IB016 Seminar on Functional Programming -- Task from lecture 01 import qualified Data.Map.Strict as M newtype Point = P { unP :: (Double, Double) } deriving (Eq, Ord, Show) type Canvas = M.Map Point Char exampleCanvas1 :: Canvas exampleCanvas1 = M.fromList [ (P (0,0), 'A') , (P (1,1), 'B') , (P (2,2), 'C') , (P (3,1), 'D') , (P (4,0), 'E') , (P (5,7), 'F') , (P (-2,-1), 'G') ] furthestFrom :: Point -> Canvas -> Maybe Char furthestFrom origin canvas = snd <$> (M.lookupMax $ M.mapKeys (distance origin) canvas) where distance (P (x1, y1)) (P (x2, y2)) = sqrt $ (x2-x1)^(2::Int) + (y2-y1)^(2::Int) linePoints :: Point -> Point -> Canvas -> [Char] linePoints (P (x1, y1)) (P (x2, y2)) canvas = M.elems $ M.filterWithKey onLine canvas where onLine (P (x, y)) _ = y - y1 == slope * (x - x1) slope = (y2 - y1) / (x2 - x1) reflectY :: Canvas -> Canvas reflectY canvas = M.union canvas $ M.mapKeys reflect canvas where reflect (P (x,y)) = P (-x, y)