Advertisement
Guest User

Untitled

a guest
Oct 11th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Ficha2 where
  2.  
  3. import Data.Char
  4. import Data.List (partition)
  5.  
  6. -- Exercicio 1
  7.  
  8. -- a
  9. funA :: [Double] -> Double
  10. funA [] = 0
  11. funA (y:ys) = y^2 + (funA ys)
  12. -- 39.0
  13.  
  14. -- b
  15. funB :: [Int] -> [Int]
  16. funB [] = []
  17. funB (h:t) =
  18.     if (mod h 2) == 0
  19.         then h : (funB t)
  20.         else (funB t)
  21. -- [8,12]
  22.  
  23.  
  24. -- c
  25. funC (x:y:t) = funC t
  26. funC [x] = []
  27. funC [] = []
  28. -- []
  29.  
  30. -- d
  31. funD l = g [] l
  32. g l [] = l
  33. g l (h:t) = g (h:l) t
  34. -- "certo"
  35.  
  36.  
  37. -- Exercicio 2
  38.  
  39. -- a
  40. dobros :: [Float] -> [Float]
  41. dobros [] = []
  42. dobros (h:t) = (2*h):(dobros t)
  43.  
  44. -- b
  45. numOcorre :: Char -> String -> Int
  46. numOcorre x [] = 0
  47. numOcorre x (c:str) =
  48.     if x==c
  49.         then 1 + numOcorre x str
  50.         else numOcorre x str
  51.  
  52. -- c
  53. positivos :: [Int] -> Bool
  54. positivos [] = False
  55. positivos (h:t) =
  56.     if(h > 0)
  57.         then positivos t
  58.         else False
  59.  
  60. -- d
  61. soPos :: [Int] -> [Int]
  62. soPos [] = []
  63. soPos (h:t) =
  64.     if h > 0
  65.         then h:soPos t
  66.         else soPos t
  67.  
  68. -- e
  69. somaNeg :: [Int] -> Int
  70. somaNeg [] = 0
  71. somaNeg (h:t) =
  72.     if(h < 0)
  73.         then h + somaNeg t
  74.         else somaNeg t
  75.  
  76. -- f
  77. tresUlt :: [a] -> [a]
  78. tresUlt (h:t) =
  79.      if (length t) + 1 > 3
  80.         then tresUlt t
  81.         else h:t
  82.  
  83. -- g
  84. segundos :: [(a,b)] -> [b]
  85. segundos [] = []
  86. segundos ((a,b):xs) = b:segundos xs
  87.  
  88. -- h
  89. nosPrimeiros :: (Eq a) => a -> [(a,b)] -> Bool
  90. nosPrimeiros x [] = False
  91. nosPrimeiros x ((a,b):xs) =
  92.     if( a == x )
  93.         then True
  94.         else nosPrimeiros x xs
  95.  
  96. -- i
  97. sumTriplos :: (Num a, Num b, Num c) => [(a,b,c)] -> (a,b,c)
  98. sumTriplos [(a,b,c)] = (a,b,c)
  99. sumTriplos ((a,b,c):xs) =
  100.     let (sumA,sumB,sumC) = sumTriplos xs
  101.     in (a+sumA,b+sumB,c+sumC)
  102.  
  103.  
  104. -- Exercicio 3
  105.  
  106. -- a
  107. soDigitos :: [Char] -> [Char]
  108. soDigitos [] = []
  109. soDigitos (h:t) =
  110.     if isDigit h
  111.         then h:soDigitos t
  112.         else soDigitos t
  113.  
  114. -- b
  115. minusculas :: [Char] -> Int
  116. minusculas [] = 0
  117. minusculas (h:t) =
  118.     if isLower h
  119.         then 1 + minusculas t
  120.         else minusculas t
  121.  
  122. -- c
  123. nums :: String -> [Int]
  124. nums [] = []
  125. nums (c:str) =
  126.     if isDigit c
  127.         then (ord c - 48):nums str
  128.         else nums str
  129.  
  130.  
  131. -- Exercicio 4
  132.  
  133. type Polinomio = [Monomio]
  134. type Monomio = (Float,Int)
  135.  
  136. -- a
  137. conta :: Int -> Polinomio -> Int
  138. conta n [] = 0
  139. conta n (h:t) =
  140.     if (snd h) == n
  141.         then 1+conta n t
  142.         else conta n t
  143.  
  144. -- b
  145. grau :: Polinomio -> Int
  146. grau [] = 0
  147. grau (h:t) =
  148.     if grau t < snd h
  149.         then snd h
  150.         else grau t
  151.  
  152. -- c
  153. selgrau :: Int -> Polinomio -> Polinomio
  154. selgrau n [] = []
  155. selgrau n (h:t) =
  156.     if snd h == n
  157.         then h:selgrau n t
  158.         else selgrau n t
  159.  
  160. -- d
  161. deriv :: Polinomio -> Polinomio
  162. deriv [] = []
  163. deriv ((f,s):t) =
  164.     if s > 0
  165.         then (f*fromIntegral s,s-1):deriv t
  166.         else (f,s):deriv t
  167. -- e
  168. calcula :: Float -> Polinomio -> Float
  169. calcula _ [] = 0
  170. calcula n ((f,s):t) = f*(n^(fromIntegral s)) + calcula n t
  171.  
  172. -- f
  173. simp :: Polinomio -> Polinomio
  174. simp [] = []
  175. simp (h:t) =
  176.     if fst h == 0
  177.         then simp t
  178.         else h:simp t
  179.  
  180. -- g
  181. mult :: Monomio -> Polinomio -> Polinomio
  182. mult _ [] = []
  183. mult (a,b) ((f,s):t) = (a*f,b+s):mult (a,b) t
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement