import Data.List ((!?)) data Point = Point Double Double deriving (Eq, Show) data Vector = Vec Double Double deriving (Eq, Show) -- Eq pre Shape je definované ručne v úlohe 2.1, preto tu je len Show data Shape = Circle Point Double | Rectangle Point Point deriving Show -- strom z minulého cvičenia data BTree a = Leaf a | Branch (BTree a) (BTree a) deriving Show myLittleTree :: BTree Integer myLittleTree = Branch (Branch (Leaf 1) (Branch (Leaf 2) (Leaf 3))) (Leaf 4) -- Úloha 1.1 -- Implementujte funkciu allEqual, ktorá vráti True ak všetky hodnoty v zozname sú rovné, alebo ak je zoznam prázdny. Aký je typ tejto funkcie? -- Typ je Eq a => [a] -> Bool: funguje pre akýkoľvek typ a, -- ktorý patrí do triedy Eq (hodnoty sa dajú porovnať cez ==). allEqual :: Eq a => [a] -> Bool allEqual [] = True allEqual (x:xs) = all (== x) xs -- Úloha 1.2 -- Upravte definíciu funkcie sumBTree z minulého cvičenia tak, aby dokázala pracovať nielen so stromami obsahujúcimi Integer, ale aj iné numerické typy, napríklad Int alebo Double -- Namiesto Integer je typová premenná a s obmedzením Num a, -- teda akýkoľvek číselný typ (Int, Integer, Double, ...). sumBTree :: Num a => BTree a -> a sumBTree (Leaf x) = x sumBTree (Branch l r) = sumBTree l + sumBTree r -- Úloha 2.1 -- Definujte inštanciu typovej triedy Eq pre typ Shape tak, aby obdĺžniky, ktoré sa líšia iba poradím bodov, ktoré ich definujú, boli považované za rovné. -- (Rectangle (Point 0 0) (Point 5 5) == Rectangle (Point 5 5) (Point 0 0)) == True instance Eq Shape where Circle c1 r1 == Circle c2 r2 = c1 == c2 && r1 == r2 Rectangle a1 b1 == Rectangle a2 b2 = normalize a1 b1 == normalize a2 b2 _ == _ = False -- prevedie obdĺžnik na (ľavý dolný roh, pravý horný roh), -- takže nezáleží na poradí bodov ani na tom, ktorú uhlopriečku zadáme normalize :: Point -> Point -> (Point, Point) normalize (Point x1 y1) (Point x2 y2) = (Point (min x1 x2) (min y1 y2), Point (max x1 x2) (max y1 y2)) -- Úloha 2.2 -- Bolo by možné definovať inštanciu Ord pre typ Shape? Aké by bolo kritérium porovnávania? -- Áno, napríklad podľa obsahu (väčší útvar je "väčší"). -- Problém: dva rôzne útvary s rovnakým obsahom by mali compare = EQ, -- ale == by vrátilo False. Ord by tak nebol v súlade s Eq, -- takže to nie je úplne čistá inštancia. area :: Shape -> Double area (Circle _ r) = pi * r^2 area (Rectangle (Point x1 y1) (Point x2 y2)) = abs (x2 - x1) * abs (y2 - y1) instance Ord Shape where compare s1 s2 = compare (area s1) (area s2) -- Vlastná typová trieda cieľ 3 -- Úloha 3.1 -- Definujte typovú triedu Movable s týmito metódami: -- move — posunie objekt (druhý argument) o zadaný vektor (prvý argument), -- reflectX — zrkadlí objekt okolo súradnicovej osi X, -- reflectY — zrkadlí objekt okolo súradnicovej osi Y, -- rotate180 — otočí objekt o 180° okolo stredu súradnicovej sústavy. class Movable a where move :: Vector -> a -> a reflectX :: a -> a reflectY :: a -> a rotate180 :: a -> a -- otočenie o 180° je to isté ako zrkadlenie podľa oboch osí rotate180 = reflectX . reflectY -- Úloha 3.2 -- Definujte inštancie triedy Movable pre Point a Shape. instance Movable Point where move (Vec dx dy) (Point x y) = Point (x + dx) (y + dy) reflectX (Point x y) = Point x (-y) reflectY (Point x y) = Point (-x) y -- útvar posunieme/zrkadlíme tak, že to urobíme s jeho bodmi instance Movable Shape where move v (Circle c r) = Circle (move v c) r move v (Rectangle a b) = Rectangle (move v a) (move v b) reflectX (Circle c r) = Circle (reflectX c) r reflectX (Rectangle a b) = Rectangle (reflectX a) (reflectX b) reflectY (Circle c r) = Circle (reflectY c) r reflectY (Rectangle a b) = Rectangle (reflectY a) (reflectY b) -- 4Functor a Foldable cieľ 2 -- Mnohé funkcie pre prácu s údajovými štruktúrami sú definované nielen pre zoznamy, ale aj pre iné štruktúry. Napríklad okrem funkcie map existuje jej všeobecnejšia forma fmap definovaná pre triedu Functor. Funkcie foldr, foldl a mnohé ďalšie tiež nie sú obmedzené iba pre zoznamu, ale sú definované pre triedu Foldable. -- Úloha 4.1 -- Definujte inštanciu typovej triedy Functor pre typ stromu. Trieda Functor deklaruje funkciu: -- class Functor f where -- fmap :: (a -> b) -> f a -> f b -- Je to všeobecnejšia verzia funkcie map. V prípade stromu zmení vstupnou funkciou každý jeho prvok. -- inštancia je pre BTree (kontajner), nie pre BTree a instance Functor BTree where fmap f (Leaf x) = Leaf (f x) fmap f (Branch l r) = Branch (fmap f l) (fmap f r) -- Úloha 4.2 -- Definujte inštanciu triedy Foldable pre typ BTree. V rámci inštancie implementujte funkciu foldr. -- Nech sa „skladanie“ začína aplikáciou zadanej funkcie na každý list stromu a počiatočnú hodnotu. Napríklad, výraz foldr (+) 0 (Branch (Branch (Leaf 1) (Leaf 2)) (Leaf 3)) sa vyhodnotí ako ((1+0) + (2+0)) + (3 + 0). -- najprv sa poskladá pravý podstrom, jeho výsledok je počiatočná hodnota pre ľavý; -- pre (+) vyjde to isté ako v zadaní (presne ten tvar sa všeobecne napísať nedá, -- lebo f :: a -> b -> b nevie spojiť dva výsledky typu b) instance Foldable BTree where foldr f z (Leaf x) = f x z foldr f z (Branch l r) = foldr f (foldr f z r) l ---------------------------------------------- -- main main :: IO () main = do putStrLn "--- 1.1: ---" print (allEqual [1, 1, 1]) print (allEqual "aab") print (allEqual ([] :: [Int])) putStrLn "--- 1.2: ---" print (sumBTree myLittleTree) print (sumBTree (Branch (Leaf 1.5) (Leaf 2.25) :: BTree Double)) putStrLn "--- 2.1: ---" print (Rectangle (Point 0 0) (Point 5 5) == Rectangle (Point 5 5) (Point 0 0)) print (Rectangle (Point 0 0) (Point 5 5) == Rectangle (Point 0 5) (Point 5 0)) print (Rectangle (Point 0 0) (Point 5 5) == Rectangle (Point 0 0) (Point 5 6)) print (Circle (Point 0 0) 1 == Rectangle (Point 0 0) (Point 1 1)) putStrLn "--- 2.2: ---" print (Circle (Point 0 0) 1 < Rectangle (Point 0 0) (Point 5 5)) print (maximum [Circle (Point 0 0) 1, Rectangle (Point 0 0) (Point 2 2)]) putStrLn "--- 3.2: ---" print (move (Vec 1 2) (Point 0 0)) print (reflectX (Point 3 4)) print (reflectY (Point 3 4)) print (rotate180 (Point 3 4)) print (move (Vec 1 1) (Rectangle (Point 0 0) (Point 2 2))) print (rotate180 (Circle (Point 1 2) 5)) putStrLn "--- 4.1: ---" print (fmap (*10) myLittleTree) print (fmap show myLittleTree) putStrLn "--- 4.2: ---" print (foldr (+) 0 (Branch (Branch (Leaf 1) (Leaf 2)) (Leaf 3))) print (foldr (:) [] myLittleTree) print (sum myLittleTree) print (length myLittleTree) print (maximum myLittleTree) print (elem 3 myLittleTree)