Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.Foldable()
- -- Exercitiul 1
- data Arbore a
- = Nod (Arbore a) a (Arbore a)
- | Vid
- -- For testing purposes.
- instance Eq a => Eq (Arbore a) where
- Vid == Vid = True
- _ == Vid = False
- Vid == _ = False
- (Nod stA a drA) == (Nod stB b drB) = a == b && stA == stB && drA == drB
- -- rootBigger x y -> checks if root of x is bigger than all the elements in y
- rootBigger :: Ord a => Arbore a -> Arbore a -> Bool
- rootBigger Vid _ = True
- rootBigger _ Vid = True
- rootBigger (Nod x a y) (Nod st other dr) = a > other && rootBigger (Nod x a y) st && rootBigger (Nod x a y) dr
- -- rootSmaller x y -> checks if root of x is smaller than all the elements in y
- rootSmaller :: Ord a => Arbore a -> Arbore a -> Bool
- rootSmaller Vid _ = True
- rootSmaller _ Vid = True
- rootSmaller (Nod x a y) (Nod st other dr) = a < other && rootSmaller (Nod x a y) st && rootSmaller (Nod x a y) dr
- -- a)
- verif :: Ord a => Arbore a -> Bool
- verif Vid = True
- verif (Nod st a dr) = rootBigger (Nod st a dr) st && rootSmaller (Nod st a dr) dr && verif st && verif dr
- arb1 :: Arbore Integer
- arb1 = Nod Vid 5 Vid
- arb2 :: Arbore Integer
- arb2 = Nod (Nod Vid 3 Vid) 5 (Nod Vid 7 Vid)
- test1 :: Bool
- test1 = verif arb1
- test2 :: Bool
- test2 = verif arb2
- test3 :: Bool
- test3 = not (verif (Nod (Nod Vid 6 Vid) 5 (Nod Vid 7 Vid)))
- test4 :: Bool
- test4 = not (verif (Nod (Nod Vid 3 Vid) 5 (Nod Vid 7 (Nod Vid 4 Vid))))
- test5 :: Bool
- test5 = not (verif (Nod (Nod Vid 3 Vid) 5 (Nod Vid 7 (Nod Vid 7 Vid))))
- -- b)
- insert :: Ord t => Arbore t -> t -> Arbore t
- insert Vid x = Nod Vid x Vid
- insert (Nod st a dr) x
- | x < a = Nod (insert st x) a dr
- | x > a = Nod st a (insert dr x)
- | otherwise = Nod st a dr
- test6 :: Bool
- test6 = insert Vid 5 == Nod Vid 5 Vid
- test7 :: Bool
- test7 = insert arb1 5 == arb1
- test8 :: Bool
- test8 = insert arb2 6 == Nod (Nod Vid 3 Vid) 5 (Nod (Nod Vid 6 Vid) 7 Vid)
- -- c)
- instance Functor Arbore where
- fmap _ Vid = Vid
- fmap f (Nod st a dr) = Nod (fmap f st) (f a) (fmap f dr)
- test9 :: Bool
- test9 = fmap (+1) arb2 == Nod (Nod Vid 4 Vid) 6 (Nod Vid 8 Vid)
- -- Exercitiul 2
- -- a)
- instance Show a => Show (Arbore a) where
- show Vid = ""
- show (Nod st a dr) = show st ++ show a ++ "," ++ show dr
- testArb :: Arbore Integer
- testArb = Vid
- testArb1 :: Arbore Integer
- testArb1 = insert testArb 1
- testArb2 :: Arbore Integer
- testArb2 = insert testArb1 5
- testArb3 :: Arbore Integer
- testArb3 = insert testArb2 2
- testArb4 :: Arbore Integer
- testArb4 = insert testArb3 0
- testArb5 :: Arbore Integer
- testArb5 = insert testArb4 6
- test10 :: Bool
- test10 = show testArb5 == "0,1,2,5,6,"
- -- b)
- instance Foldable Arbore where
- foldMap _ Vid = mempty
- foldMap f (Nod st a dr) = foldMap f st `mappend` f a `mappend` foldMap f dr
- test11 :: Bool
- test11 = foldr (+) 5 testArb5 == 19
- test12 :: Bool
- test12 = sum testArb4 == 8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement