Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Ficha2 where
- import Data.Char
- import Data.List (partition)
- -- Exercicio 1
- -- a
- funA :: [Double] -> Double
- funA [] = 0
- funA (y:ys) = y^2 + (funA ys)
- -- 39.0
- -- b
- funB :: [Int] -> [Int]
- funB [] = []
- funB (h:t) =
- if (mod h 2) == 0
- then h : (funB t)
- else (funB t)
- -- [8,12]
- -- c
- funC (x:y:t) = funC t
- funC [x] = []
- funC [] = []
- -- []
- -- d
- funD l = g [] l
- g l [] = l
- g l (h:t) = g (h:l) t
- -- "certo"
- -- Exercicio 2
- -- a
- dobros :: [Float] -> [Float]
- dobros [] = []
- dobros (h:t) = (2*h):(dobros t)
- -- b
- numOcorre :: Char -> String -> Int
- numOcorre x [] = 0
- numOcorre x (c:str) =
- if x==c
- then 1 + numOcorre x str
- else numOcorre x str
- -- c
- positivos :: [Int] -> Bool
- positivos [] = False
- positivos (h:t) =
- if(h > 0)
- then positivos t
- else False
- -- d
- soPos :: [Int] -> [Int]
- soPos [] = []
- soPos (h:t) =
- if h > 0
- then h:soPos t
- else soPos t
- -- e
- somaNeg :: [Int] -> Int
- somaNeg [] = 0
- somaNeg (h:t) =
- if(h < 0)
- then h + somaNeg t
- else somaNeg t
- -- f
- tresUlt :: [a] -> [a]
- tresUlt (h:t) =
- if (length t) + 1 > 3
- then tresUlt t
- else h:t
- -- g
- segundos :: [(a,b)] -> [b]
- segundos [] = []
- segundos ((a,b):xs) = b:segundos xs
- -- h
- nosPrimeiros :: (Eq a) => a -> [(a,b)] -> Bool
- nosPrimeiros x [] = False
- nosPrimeiros x ((a,b):xs) =
- if( a == x )
- then True
- else nosPrimeiros x xs
- -- i
- sumTriplos :: (Num a, Num b, Num c) => [(a,b,c)] -> (a,b,c)
- sumTriplos [(a,b,c)] = (a,b,c)
- sumTriplos ((a,b,c):xs) =
- let (sumA,sumB,sumC) = sumTriplos xs
- in (a+sumA,b+sumB,c+sumC)
- -- Exercicio 3
- -- a
- soDigitos :: [Char] -> [Char]
- soDigitos [] = []
- soDigitos (h:t) =
- if isDigit h
- then h:soDigitos t
- else soDigitos t
- -- b
- minusculas :: [Char] -> Int
- minusculas [] = 0
- minusculas (h:t) =
- if isLower h
- then 1 + minusculas t
- else minusculas t
- -- c
- nums :: String -> [Int]
- nums [] = []
- nums (c:str) =
- if isDigit c
- then (ord c - 48):nums str
- else nums str
- -- Exercicio 4
- type Polinomio = [Monomio]
- type Monomio = (Float,Int)
- -- a
- conta :: Int -> Polinomio -> Int
- conta n [] = 0
- conta n (h:t) =
- if (snd h) == n
- then 1+conta n t
- else conta n t
- -- b
- grau :: Polinomio -> Int
- grau [] = 0
- grau (h:t) =
- if grau t < snd h
- then snd h
- else grau t
- -- c
- selgrau :: Int -> Polinomio -> Polinomio
- selgrau n [] = []
- selgrau n (h:t) =
- if snd h == n
- then h:selgrau n t
- else selgrau n t
- -- d
- deriv :: Polinomio -> Polinomio
- deriv [] = []
- deriv ((f,s):t) =
- if s > 0
- then (f*fromIntegral s,s-1):deriv t
- else (f,s):deriv t
- -- e
- calcula :: Float -> Polinomio -> Float
- calcula _ [] = 0
- calcula n ((f,s):t) = f*(n^(fromIntegral s)) + calcula n t
- -- f
- simp :: Polinomio -> Polinomio
- simp [] = []
- simp (h:t) =
- if fst h == 0
- then simp t
- else h:simp t
- -- g
- mult :: Monomio -> Polinomio -> Polinomio
- mult _ [] = []
- mult (a,b) ((f,s):t) = (a*f,b+s):mult (a,b) t
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement