-
Notifications
You must be signed in to change notification settings - Fork 1
/
20150407.hs
85 lines (69 loc) · 2.43 KB
/
20150407.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
data Shape = Circle Float | Rectangle Float Float
area :: Shape -> Float
area (Circle r) = pi*r*r
area (Rectangle c l) = c*l
type HorasAula = Int
type Disciplina = [String]
--Segunda, Terca, Quarta, Quinta, Sexta, Sabado e Domingo são construtores
data Dia = Segunda HorasAula Disciplina| Terca HorasAula Disciplina| Quarta HorasAula Disciplina| Quinta HorasAula Disciplina| Sexta HorasAula Disciplina| Sabado | Domingo
ehFDS :: Dia -> Bool
ehFDS Sabado = True
ehFDS Domingo = True
ehFDS _ = False
-- definicão preguiçosa
temPLC :: Dia -> Bool
temPLC (Terca _ _) = True
temPLC (Quinta _ _) = True
temPLC (_ ) = False
-- definicão não preguiçosa
plcHoje :: Dia -> Bool
plcHoje Sabado = False
plcHoje Domingo = False
plcHoje (Segunda h dis) = contido dis "PLC"
plcHoje (Terca h dis) = contido dis "PLC"
plcHoje (Quinta h dis) = contido dis "PLC"
plcHoje (Quarta h dis) = contido dis "PLC"
plcHoje (Sexta h dis) = contido dis "PLC"
contido :: Disciplina -> String -> Bool
contido [] xis = False
contido (a:as) xis
|a == xis = True
|otherwise = contido as xis
data Expr = Lit Int |
Add Expr Expr |
Sub Expr Expr
data List t = Nul |
Cons t (List t)
deriving (Show)
data Tree t = NulT |
Node t (Tree t) (Tree t)
deriving (Eq, Show)
--ex. de teste usando construtor showExpr (Add (Lit 7) (Lit 6))
showExpr :: Expr -> String
showExpr (Lit x) = show x
showExpr (Add a b) = showExpr a ++ " + " ++ showExpr b
showExpr (Sub a b) = showExpr a ++ " - " ++ showExpr b
--ex. teste toList (Cons 8 (Nul)) / toList (Cons 8 (Cons 4 (Cons 6 (Nul))))
toList :: List t -> [t]
toList (Nul) = []
toList (Cons x ls) = (x:(toList ls))
fromList :: (Show t) => [t] -> List t
fromList [] = Nul
fromList (l:ls) = (Cons l (fromList ls))
-- teste: depth (Node 5 (Node 3 (NulT) (NulT)) (Node 2 (Node 3 (NulT) (NulT)) (NulT)))
--estrutura basica: depth (Node 4 (NulT) (NulT))
depth :: Tree t -> Int
depth NulT = 0
depth (Node c a b) = 1 + (max (depth a) (depth b))
--não sei o que collapse faz
--collapse :: Tree t -> [t]
-- teste: bfs (Node 5 (Node 8 (NulT) (NulT)) (Node 3 (Node 2 (NulT) (NulT)) (NulT))) 2
bfs :: (Eq t) => Tree t -> t -> Bool
bfs (NulT) v = False
bfs (Node n a b) v
|n == v = True
|otherwise = (bfs a v) || (bfs b v)
--mapTree (2*) (Node 5 (Node 3 (NulT) (NulT)) (Node 2 (Node 3 (NulT) (NulT)) (NulT)))
mapTree:: (t -> u) -> Tree t -> Tree u
mapTree fun NulT = NulT
mapTree fun (Node x a b) = Node (fun x) (mapTree fun a) (mapTree fun b)