-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20150409.hs
96 lines (61 loc) · 2.13 KB
/
20150409.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import Data.Char
--Trabalho 6
-- Método 1
--Exercicio 1
data Arv1 t = Vazio1 | Arv1 ([t], [(t,t)]) [t] deriving (Eq, Show)
ar = Arv1 ([1,2,3,4,5], [(1,2), (1,3), (3,4), (3,5)]) [10, 10, 10, 10]
--Exercicio 2
fun :: Eq t => Arv1 t -> t -> Bool
fun (Vazio1) _ = False
fun (Arv1 (v, e) p) n = (rec (Arv1 (v, e) p) [n]) /= []
rec :: Eq t => Arv1 t -> [t] -> [t]
rec (Arv1 (_,_) _) [] = []
rec (Arv1 ([],_) _) _ = []
rec (Arv1 (v,e) p) (a:as)
| [x| x <- v, (x==a)] == [] = rec (Arv1 (novoVert, e) p) as
| otherwise = a : (rec (Arv1 (novoVert,e) p) (adjacente ++ as))
where
adjacente = [x| (x,y) <- e, (y==a)] ++ [x| (y,x) <- e, (y==a)]
novoVert = [x| x <- v, (x /= a)]
-- Método 2
--Exercicio 1
data Arv t = Vazio | No t [(Int, Arv t)] deriving (Eq, Show)
grafo = (No 1 [(1, (No 3 [(1, (No 5 [(0, Vazio)])), (1, (No 6 [(0, Vazio)]))])), (1, (No 2 [(1, (No 4 [(0, Vazio)])), (1, (No 3 [(1, (No 5 [(0, Vazio)])), (1, (No 6 [(0, Vazio)]))]))])), (1, (No 7 [(0, Vazio)]))])
--Exercicio 2
dfs :: (Eq t) => Arv t -> t -> Bool
dfs (No a list) n = theDfs n [((No a list))] [a]
theDfs :: (Eq t) => t -> [Arv t] -> [t] -> Bool
theDfs n [] _ = False
theDfs n ((No topo subs):stail) visited
| topo == n = True
| otherwise = (theDfs n ([ (No val lt) | (number, (No val lt)) <- subs, not (wasVisited val visited)] ++ stail) (topo:visited))
wasVisited :: (Eq t) => t -> [t] -> Bool
wasVisited n visited = [ a | a<-visited, n==a] /= []
-- Exercicios aula
--(i)
map1 :: (t -> u) -> [t] -> [u]
map1 f [] = []
map1 f (a:as) = (f a):(map1 f as)
raiz :: Double -> Double
raiz n = sqrt n
calcular n = map1 raiz n
--(ii)
posicao :: Char -> Int
posicao x = ord x - ord 'a' + 1
posicaoAlfabeto s = map1 posicao s
--(iii)
map2 :: (t -> u) -> [t] -> [u]
map2 f (a:as) = [(f x)| x <- (a:as)]
--
fold :: (t -> t -> t) -> [t] -> t
fold f [a] = a
fold f (a:as) = f a (fold f as)
foldr' :: (t -> u -> u) -> u -> [t] -> u
foldr' f s [] = s
foldr' f s (a:as) = f a (foldr f s as)
-- Exercicios folding
--(1)
member :: Eq t => t -> [t] -> Bool
member s lista = foldr' (||) False (map (==s) lista) --[a==s| a <- lista]
--(2)
--union :: Eq t => [t] -> [t] -> [t]