-
Notifications
You must be signed in to change notification settings - Fork 0
/
ficha6.hs
148 lines (120 loc) · 4.67 KB
/
ficha6.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
module Ficha6 where
import Data.Char
import Data.List
psany :: (a->Bool) -> [a] -> Bool
psany p [] = False
psany p (h:t) = p h || psany p t
psanyOdd :: [Int] -> Bool
psanyOdd [] = False
psanyOdd (h:t) = odd h || psanyOdd t
mytakeWhile :: (a->Bool) -> [a] -> [a]
mytakeWhile p [] = []
mytakeWhile p (h:t) | p h = h : mytakeWhile p t
| otherwise = []
mydropWhile :: (a->Bool) -> [a] -> [a]
mydropWhile p [] = []
mydropWhile p (h:t) | p h = mydropWhile p t
| otherwise = h:t
myzipWith :: (a->b->c) -> [a] -> [b] -> [c]
myzipWith p _ [] = []
myzipWith p [] _ = []
myzipWith p (h:t) (h1:t1) =(p h h1) : (myzipWith p t t1)
mydeleteBy :: (a->a->Bool) -> a -> [a] -> [a]
mydeleteBy p x [] = []
mydeleteBy p x (h:t) | p x h = t
| otherwise = h : mydeleteBy p x t
mysortOn :: Ord b => (a->b) -> [a] -> [a]
mysortOn p [] = []
mysortOn p (h:t) = myinsert h (mysortOn p t)
where myinsert x [] = [x]
myinsert x (y:ys) | p x <= p y = x:y:ys
| otherwise = y : myinsert x ys
myspan :: (a->Bool) -> [a] -> ([a],[a])
myspan _ [] = ([],[])
myspan p (h:t) = if (p h) then (h:a,b) else ([],(h:t))
where (a,b) = myspan p t
type Polinomio = [Monomio]
type Monomio = (Float,Int)
selgrau :: Int -> Polinomio -> Polinomio
selgrau _ [] = []
selgrau grau poli = filter aux poli
where aux :: Monomio -> Bool
aux (x,y) = y==grau
conta :: Int -> Polinomio -> Int
conta grau poli = length (filter (\(x,y)->y==grau) poli)
{-mesma coisa mas com foldr.
conta grau poli = foldr aux 0 poli
where aux (x,y) r = if y == grau then 1 + r else r -}
grau :: Polinomio -> Int
grau poli = maximum (map snd poli)
deriv :: Polinomio -> Polinomio
deriv poli = (map aux poli)
where aux :: Monomio -> Monomio
aux (x,y) | y > 0 = ((x * fromIntegral y),(y-1))
| otherwise = (0,0)
calcula :: Float -> Polinomio -> Float
calcula n poli = sum (map (\(x,y)->x*n^y) poli)
simp :: Polinomio -> Polinomio
simp poli = filter (\(x,y)-> y/=0) poli
{-
mult :: Monomio -> Polinomio -> Polinomio
mult mono (h:t) = foldr (*) mono fromIntegral h : mult t -}
mult2 :: Monomio -> Polinomio -> Polinomio
mult2 (c,e) poli = map (\(x,y)-> (x * c , fromIntegral e + y)) poli
ordena :: Polinomio -> Polinomio
ordena poli = mysortOn snd poli
normaliza :: Polinomio -> Polinomio
normaliza [] = []
normaliza (h:t) = acresAux h (normaliza t)
where acresAux :: Monomio -> Polinomio -> Polinomio
acresAux (c,e) [] = [(c,e)]
acresAux (c,e) ((x,y):t) | e == y = (c+x,e) : t
| otherwise = (x,y) : (acresAux (c,e) t)
{-normaliza2 :: Polinomio -> Polinomio
normaliza2 (h:t) = foldr p h t : normaliza2 t
where p :: Monomio -> Polinomio -> Polinomio
p (c,e) [] = [(c,e)]
p (c,e) ((x,y):t) | e == y = (c+x,e) : t
| otherwise = (x,y) : (p (c,e) t) -}
soma :: Polinomio -> Polinomio -> Polinomio
soma [] poli = poli
soma poli [] = poli
soma (h:t) poli = soma t (acresAux h poli)
where acresAux :: Monomio -> Polinomio -> Polinomio
acresAux (c,e) [] = [(c,e)]
acresAux (c,e) ((x,y):t) | e == y = (c+x,e) : t
| otherwise = (x,y) : (acresAux (c,e) t)
produto :: Polinomio -> Polinomio -> Polinomio
produto [] _ = []
produto _ [] = []
produto (h:t) poli = soma (produtoAux h poli) (produto t poli)
where produtoAux :: Monomio -> Polinomio -> Polinomio
produtoAux _ [] = []
produtoAux (c,e) ((a,b):t) = (c * a, e + b) : produtoAux (c,e) t
equiv :: Polinomio -> Polinomio -> Bool
equiv [] [] = True
equiv poli poli2 | h == h1 = equiv t t1
| otherwise = False
where (h:t) = ordena poli
(h1:t1) = ordena poli2
type Mat a = [[a]]
dimOK :: Mat a -> Bool
dimOK [] = True
dimOK (h:t) = aux (length h) t
where aux :: Int -> [[a]] -> Bool
aux x [] = True
aux x (h:t) | x == length h = aux x t
| otherwise = False
dimMat :: Mat a -> (Int,Int)
dimMat [] = (0,0)
dimMat (h:t) = (length h, length (h:t))
addMat :: Num a => Mat a -> Mat a -> Mat a
addMat [] [] = []
addMat matriz matriz2 = zipWith (zipWith (+)) matriz matriz2
{-
mytranspose :: Mat a -> Mat a
mytranspose [] = []
mytranspose (h:t) = (map aux (h:t)) : mytranspose t
where aux :: Mat a -> [a]
aux [] = []
aux ((h:t):t1) = h : aux t1 -}