Advertisement
Mihai_Preda

Untitled

Jan 31st, 2021
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.Foldable()
  2.  
  3. -- Exercitiul 1
  4.  
  5. data Arbore a
  6.     = Nod (Arbore a) a (Arbore a)
  7.     | Vid
  8.  
  9. -- For testing purposes.
  10. instance Eq a => Eq (Arbore a) where
  11.     Vid == Vid = True
  12.     _ == Vid = False
  13.     Vid == _ = False
  14.     (Nod stA a drA) == (Nod stB b drB) = a == b && stA == stB && drA == drB
  15.  
  16. -- rootBigger x y -> checks if root of x is bigger than all the elements in y
  17. rootBigger :: Ord a => Arbore a -> Arbore a -> Bool
  18. rootBigger Vid _ = True
  19. rootBigger _ Vid = True
  20. rootBigger (Nod x a y) (Nod st other dr) = a > other && rootBigger (Nod x a y) st && rootBigger (Nod x a y) dr
  21.  
  22. -- rootSmaller x y -> checks if root of x is smaller than all the elements in y
  23. rootSmaller :: Ord a => Arbore a -> Arbore a -> Bool
  24. rootSmaller Vid _ = True
  25. rootSmaller _ Vid = True
  26. rootSmaller (Nod x a y) (Nod st other dr) = a < other && rootSmaller (Nod x a y) st && rootSmaller (Nod x a y) dr
  27.  
  28. -- a)
  29. verif :: Ord a => Arbore a -> Bool
  30. verif Vid = True
  31. verif (Nod st a dr) = rootBigger (Nod st a dr) st && rootSmaller (Nod st a dr) dr && verif st && verif dr
  32.  
  33. arb1 :: Arbore Integer
  34. arb1 = Nod Vid 5 Vid
  35. arb2 :: Arbore Integer
  36. arb2 = Nod (Nod Vid 3 Vid) 5 (Nod Vid 7 Vid)
  37.  
  38. test1 :: Bool
  39. test1 = verif arb1
  40.  
  41. test2 :: Bool
  42. test2 = verif arb2
  43.  
  44. test3 :: Bool
  45. test3 = not (verif (Nod (Nod Vid 6 Vid) 5 (Nod Vid 7 Vid)))
  46.  
  47. test4 :: Bool
  48. test4 = not (verif (Nod (Nod Vid 3 Vid) 5 (Nod Vid 7 (Nod Vid 4 Vid))))
  49.  
  50. test5 :: Bool
  51. test5 = not (verif (Nod (Nod Vid 3 Vid) 5 (Nod Vid 7 (Nod Vid 7 Vid))))
  52.  
  53. -- b)
  54. insert :: Ord t => Arbore t -> t -> Arbore t
  55. insert Vid x = Nod Vid x Vid
  56. insert (Nod st a dr) x
  57.     | x < a = Nod (insert st x) a dr
  58.     | x > a = Nod st a (insert dr x)
  59.     | otherwise = Nod st a dr
  60.  
  61. test6 :: Bool
  62. test6 = insert Vid 5 == Nod Vid 5 Vid
  63.  
  64. test7 :: Bool
  65. test7 = insert arb1 5 == arb1
  66.  
  67. test8 :: Bool
  68. test8 = insert arb2 6 == Nod (Nod Vid 3 Vid) 5 (Nod (Nod Vid 6 Vid) 7 Vid)
  69.  
  70. -- c)
  71. instance Functor Arbore where
  72.     fmap _ Vid = Vid
  73.     fmap f (Nod st a dr) = Nod (fmap f st) (f a) (fmap f dr)
  74.  
  75. test9 :: Bool
  76. test9 = fmap (+1) arb2 == Nod (Nod Vid 4 Vid) 6 (Nod Vid 8 Vid)
  77.  
  78.  
  79. -- Exercitiul 2
  80.  
  81. -- a)
  82. instance Show a => Show (Arbore a) where  
  83.     show Vid = ""
  84.     show (Nod st a dr) = show st ++ show a ++ "," ++ show dr
  85.  
  86. testArb :: Arbore Integer
  87. testArb = Vid
  88. testArb1 :: Arbore Integer
  89. testArb1 = insert testArb 1
  90. testArb2 :: Arbore Integer
  91. testArb2 = insert testArb1 5
  92. testArb3 :: Arbore Integer
  93. testArb3 = insert testArb2 2
  94. testArb4 :: Arbore Integer
  95. testArb4 = insert testArb3 0
  96. testArb5 :: Arbore Integer
  97. testArb5 = insert testArb4 6
  98.  
  99. test10 :: Bool
  100. test10 = show testArb5 == "0,1,2,5,6,"
  101.  
  102. -- b)
  103. instance Foldable Arbore where
  104.     foldMap _ Vid = mempty
  105.     foldMap f (Nod st a dr) = foldMap f st `mappend` f a `mappend` foldMap f dr
  106.  
  107. test11 :: Bool
  108. test11 = foldr (+) 5 testArb5 == 19
  109.  
  110. test12 :: Bool
  111. test12 = sum testArb4 == 8
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement