Guest User

Untitled

a guest
Jul 20th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Definitions where
  2.  
  3. data MyNum = Zero | Succ MyNum
  4.  
  5. instance Eq MyNum where
  6.     Zero == Zero = True
  7.     (Succ a) == (Succ b) = a == b
  8.     _ == _ = False
  9.  
  10. instance Ord MyNum where
  11.     _ <= Zero = False
  12.     Zero <= _ = True
  13.     (Succ a) <= (Succ b) = a <= b
  14.    
  15. instance Show MyNum where
  16.     show Zero = show 0
  17.     show (Succ a) = show (1 + read(show a) :: Int)
  18.  
  19. {-
  20. Functie ce converteste un numar natural intr-un MyNum
  21. -}
  22. std2MyNum x
  23.     | x == 0 = Zero
  24.     | otherwise = (Succ (std2MyNum (x - 1)))
  25.    
  26.    
  27. -------------------------------------------------------------------------------------------
  28.  
  29. {-
  30. Arbore generic, polimorfic
  31. Arbore in care fiecare nod poate avea oricati fii
  32. -}
  33. data Tree a = EmptyTree |
  34.               Node { parent :: Maybe (Tree a),
  35.                      value :: a,
  36.                      children :: [(Tree a)]
  37.                    } deriving Eq
  38.  
  39. {-
  40. Clasa pentru containere generice pentru care se poate obtine lista valorilor. Observati
  41. cum "container" este de fapt un constructor de tip. El primeste un alt tip
  42. ca parametru "container a" si se obtine astfel un tip concret.
  43. -}
  44.  
  45. class Listable container where
  46.     toList :: container a -> [a]
  47.  
  48. {-
  49. Inrolam tipul nostru arbore la clasa Listable. Pentru asta facem o parcurgere
  50. de tipul "root first, children after"
  51. -}
  52.  
  53. instance Listable Tree where
  54.     toList EmptyTree = []
  55.     toList (Node {value=v, children=cs}) = v : concatMap toList cs
  56.  
  57.  
  58. {-
  59. Functie ce intoarce adancimea la care se gaseste un nod
  60. -}
  61. getNodeDepth EmptyTree = error "Empty trees have no depth"
  62. getNodeDepth (Node {parent = Nothing}) = 0
  63. getNodeDepth (Node {parent = Just p}) = 1 + getNodeDepth p
  64.  
  65.  
  66. {-
  67. Vizualizarea arborelui ca String
  68. -}
  69. instance (Eq a, Show a) => Show (Tree a) where
  70.     show EmptyTree = ""
  71.     show t = showNode 0 t
  72.         where
  73.             showNode indent n =
  74.                 replicate indent '\t' ++
  75.                 show (value n) ++ "\n" ++
  76.                 concatMap (showNode (indent + 1)) (children n)
  77.  
  78.  
  79. {- Pentru a putea valori de tip Tree, ne vom folosi de cateva functii ajutatoare -}
  80.  
  81. {- Functia node primeste ca prim parametru o valoare, o lista de functii care primesc ca parametru
  82. un posibil parinte si intorc un nod care are drept parinte argumentul dat si un argument care specifica
  83. parintele nodului si intoarce un nod cu valoarea data si cu parintele dat, si cu copii construiti pe
  84. baza functiilor date -}
  85. node :: a -> [Maybe (Tree a) -> Tree a] -> Maybe (Tree a) -> Tree a
  86. node v fs parent =
  87.     let self = Node parent v ([f (Just self) | f <- fs])
  88.     in  self
  89.  
  90. {- O functie derivata din functia node, pentru noduri radacina, fara parinti -}
  91. root :: a -> [Maybe (Tree a) -> Tree a] -> Tree a
  92. root v fs = node v fs Nothing
  93.  
  94. {- O functie derivata din functia node, pentru noduri fara copii, de tip frunza -}
  95. leaf :: a -> Maybe (Tree a) -> Tree a
  96. leaf v parent = node v [] parent
  97.  
  98.  
  99. -- Folosind functiile de mai sus si proprietatea functiilor Haskell de a fi curry, putem construi
  100. -- arbori foarte simplu, ca mai jos
  101. t =
  102.     root (-2) [
  103.         leaf 1,
  104.         node 3 [
  105.             leaf 3,
  106.             leaf (-5)
  107.         ]]
Add Comment
Please, Sign In to add comment