-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiposAlgebricos.hs
48 lines (34 loc) · 1.23 KB
/
tiposAlgebricos.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
data Expr = Lit Int |
Add Expr Expr |
Sub Expr Expr
deriving Show
data List t = Nil | Cons t (List t)
deriving (Eq,Ord,Show)
data Tree t = NilT |
Node t (Tree t) (Tree t)
deriving (Eq,Ord,Show)
-- Defina as seguintes funções
showExpr :: Expr -> String
showExpr (Lit n) = show n
showExpr (Add a b) = showExpr a ++ " + " ++ showExpr b
showExpr (Sub a b) = showExpr a ++ " - " ++ showExpr b
toList :: List t -> [t]
toList Nil = []
toList (Cons n l) = n : (toList l)
fromList :: [t] -> List t
fromList [] = Nil
fromList (a:as) = Cons a (fromList as)
depth :: Tree t -> Int
depth NilT = 0
depth (Node n l r) = 1 + maxi (depth l) (depth r)
where maxi a b
| a > b = a
| otherwise = b
colapse :: Tree t -> [t]
colapse NilT = []
colapse (Node n l r) = [n] ++ colapse l ++ colapse r
mapTree :: (t -> u) -> Tree t -> Tree u
mapTree f NilT = NilT
mapTree f (Node n l r) = Node (f n) (mapTree f l) (mapTree f r)
tree1 = (Node 1 (Node 2 (NilT) (NilT)) (Node 3 (NilT) (NilT)))
tree2 = (Node "abrir" (Node "fechar" (NilT) (NilT)) (Node "exercitar" (NilT) (NilT)))