Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Main where
- -- Exercicio 1
- enumFromTo' :: Int -> Int -> [Int]
- enumFromTo' m n = if m > n then [] else m : enumFromTo' (m+1) n
- enumFromTo'' :: Int -> Int -> [Int]
- enumFromTo'' m n
- | m > n = []
- | otherwise = m : enumFromTo'' (m+1) n
- -- Exercicio 2
- enumFromThenTo' :: Int -> Int -> Int -> [Int]
- enumFromThenTo' x y z
- | x > z = []
- | otherwise = x : enumFromThenTo' y (y + (y-x)) z
- -- Exercicio 3
- -- contact
- concatLista :: [a] -> [a] -> [a]
- concatLista [] ys = ys
- concatLista (xs:xss) l = xs : concatLista xss l
- -- Exercicio 4
- last' :: [a] -> a
- last' [] = error "Lista vazia"
- last' [x] = x
- last' (x:xs) = last' xs
- -- Exercicio 5
- init' :: [a] -> [a]
- init' [a] = []
- init' (xs:xss) = xs : init' xss
- -- Exercicio 6
- -- Devolve elemento x de uma lista
- devolveElemento :: [a] -> Int -> a
- devolveElemento (x:xs) i
- | i == 0 = x
- | otherwise = devolveElemento (xs) (i-1)
- -- Exercicio 7
- -- inverter a ordem de uma lista
- inverte :: [a] -> [a]
- inverte [] = []
- inverte (x:xs) = (inverte xs) ++ [x]
- -- Exercicio 8
- -- Obtem uma lista com o x primeiros elementos
- take' :: Int -> [a] -> [a]
- take' _ [] = []
- take' i (x:xs)
- | i == 0 = xs
- | otherwise = x : take' (i-1) xs
- -- Exercicio 9
- -- drop
- drop' :: Int -> [a] -> [a]
- drop' _ [] = []
- drop' n (x:xs)
- | n <= 0 = x:xs
- | otherwise = drop' (n-1) xs
- -- Exercicio 10
- -- constroi uma lista de pares a partir de duas listas
- zip' :: [a] -> [b] -> [(a, b)]
- zip' [] _ = []
- zip' _ [] = []
- zip' (x:xs) (y:ys) = (x,y): zip' xs ys
- -- Exercicio 11
- -- Testa se um elemento esta na lista
- -- definicao recursiva de elem
- elem' :: Eq a => a -> [a] -> Bool
- elem' n [] = False
- elem' n (x:xs)
- | n == x = True
- | otherwise = elem' n xs
- -- Exercicio 12
- -- Dado um elemento n e um inteiro x constroi constriu uma lista com n
- -- elementos todos iguais a x
- -- definicao recursiva de replicate
- replicate' :: Int -> a -> [a]
- replicate' n x
- | n == 0 = []
- | otherwise = x : replicate' (n-1) x
- -- Exercicio 13
- -- intersperse
- intersperse' :: a -> [a] -> [a]
- intersperse' n [x] = [x]
- intersperse' n (x:xs) = x:n:(intersperse' n xs)
- -- Exercicio 14
- group' :: Eq a => [a] -> [[a]]
- group' [] = []
- group' (x:xs) = (takeWhile (==x) xs) : group' (dropWhile (==x) xs)
- group'' :: Eq a => [a] -> [[a]]
- group'' [] = []
- group'' (x:xs) = prefix : group'' suffix
- where
- head' = x
- prefix = [ n | n <- (x:xs), n == head' ]
- suffix = [ n | n <- (x:xs), n /= head' ]
- -- Exercicio 15
- -- contact
- concat' :: [[a]] -> [a]
- concat' [] = []
- concat' (x:xs) = x ++ (concat' xs)
- -- Exercicio 16
- -- inits
- inits' :: [a] -> [[a]]
- inits' [] = [[]]
- inits' (xs:xss) = inits' (init (xs:xss)) ++ [ xs:xss ]
- -- Exercicio 17
- -- tails
- tails' :: [a] -> [[a]]
- tails' [] = [[]]
- tails' (xs:xss) = [(xs:xss)] ++ tails' (tail (xs:xss))
Add Comment
Please, Sign In to add comment