-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20150326.hs
123 lines (98 loc) · 3.81 KB
/
20150326.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
-- Trabalho 3 (26/03/2015)
type HashTable v k = [(v, k)]
myHashTable :: HashTable String Int
myHashTable = [("Introduction to Linear Algebra", 11), ("Singular Value Decomposition", 5), ("Fate/Stay Night", 19), ("Mateus", 3)]
hasKey :: (Eq k) => HashTable v k -> k -> Bool
hasKey table key
| (length table) == 0 = False
| (key == snd (head table)) = True
| otherwise = hasKey (tail table) key
get :: (Eq k) => HashTable v k -> k -> v
get table key
| not (hasKey table key) = error "Key does not exist."
| snd (head table) == key = fst (head table)
| otherwise = get (tail table) key
put :: (Eq k) => HashTable v k -> v -> k -> HashTable v k
put table value key
| (hasKey table key) = error "Key already exists."
| otherwise = table ++ [(value, key)]
remove :: (Eq k) => HashTable v k -> k -> HashTable v k
remove table key
| not (hasKey table key) = error "Key does not exist."
| key == snd (head table) = tail table
| otherwise = [(head table)] ++ remove (tail table) key
listToSet :: (Eq t) => [t] -> [t]
listToSet list
| (length list) == 0 = []
| otherwise = [head list] ++ listToSet [x | x <- (tail list), not (x == (head list))]
-- Desta vez irei utilizar casamento de padrões
intersectionLength :: (Eq t) => [t] -> [t] -> Int
intersectionLength [] _ = 0
intersectionLength _ [] = 0
intersectionLength (a:tSetA) setB = length [x | x <- setB, x == a] + intersectionLength tSetA setB
-- intersectionLength setA setB
-- | ((length setA) == 0) || ((length setB) == 0) = 0
-- | otherwise = length [x | x <- setB, x == (head setA)] + intersectionLength (tail setA) setB
comparaConjuntos :: (Eq t) => [t] -> [t] -> String
comparaConjuntos listA listB
| lAIntB == 0 = "Conjuntos disjuntos."
| (lAIntB == (length setA)) && (lAIntB == (length setB)) = "A igual a B."
| (lAIntB == (length setA)) = "B contem A."
| (lAIntB == (length setB)) = "A contem B."
| otherwise = "A interseciona B"
where
setA = listToSet listA
setB = listToSet listB
lAIntB = intersectionLength setA setB
-- ## Exercícios em sala (26/03/2015) ##
-- Função: take
-- Devolve os n primeiros elementos de uma lista
take2 :: [t] -> Int -> [t]
take2 list n
| n == 0 = []
| otherwise = (head list):(take2 (tail list) (n-1))
-- Função: drop
-- Remove os n primeiros elementos de uma lista
drop2 :: [t] -> Int -> [t]
drop2 list n
| n == 0 = list
| otherwise = drop2 (tail list) (n-1)
-- Função: takeWhile
-- Devolve os n primeiros elementos da lista que satisfaçam um predicado
takeWhile2 :: [t] -> (t -> Bool) -> [t]
takeWhile2 list p
| not (p (head list)) = []
| otherwise = (head list):(takeWhile2 (tail list) p)
-- Função: dropWhile
-- Remove os n primeiros elementos da lista que satisfaçam um predicado
dropWhile2 :: [t] -> (t -> Bool) -> [t]
dropWhile2 list p
| not (p (head list)) = list
| otherwise = dropWhile2 (tail list) p
-- Função: quicksort (ordenar tipos genéricos)
quicksort :: (Ord t) => [t] -> [t]
quicksort list
| length list < 2 = list
| otherwise = (quicksort [x | x <- (tail list), x <= (head list)]) ++ [head list] ++ (quicksort [x | x <- (tail list), x > (head list)])
-- Função: agrupar
-- Funções auxiliares: joinLists, countElem, mainAux
joinLists :: (Eq t) => [[t]] -> [t]
joinLists lists
| lists == [] = []
| otherwise = (head lists) ++ joinLists (tail lists)
countElem :: (Eq t) => [t] -> t -> Int
countElem list elem
| list == [] = 0
| (head list) == elem = 1 + countElem (tail list) elem
| otherwise = countElem (tail list) elem
mainAux :: (Eq t) => [t] -> [t] -> [(t, Int)]
mainAux list set
| set == [] = []
| otherwise = [((head set), (countElem list (head set)))] ++ (mainAux newList (tail set))
where
newList = [x | x <- list, not (x == (head set))]
agrupar :: (Eq t) => [[t]] -> [(t, Int)]
agrupar lists = mainAux fullList fullSet
where
fullList = joinLists lists
fullSet = listToSet fullList