Skip to content

Instantly share code, notes, and snippets.

@washingtonsoares
Created June 9, 2016 00:57
Show Gist options
  • Save washingtonsoares/bf8ab7e93810394224c275ba4e0920c1 to your computer and use it in GitHub Desktop.
Save washingtonsoares/bf8ab7e93810394224c275ba4e0920c1 to your computer and use it in GitHub Desktop.
import Data.Char
-- consts
mapear :: (a -> b) -> [a] -> [b]
mapear f [] = []
mapear f (x:xs) = f x : mapear f xs
filtrar :: (a -> Bool) -> [a] -> [a]
filtrar p [] = []
filtrar p (x:xs)
| p x = x : filtrar p xs
| otherwise = filtrar p xs
reduzir :: (a -> b -> b) -> b -> [a] -> b
reduzir f z [] = z
reduzir f z (x:xs) = f x (reduzir f z xs)
-- 1
primeiros :: [(a,b)] -> [a]
primeiros [] = []
primeiros (p:r) = fst(p):primeiros(r)
primeiros_map :: [(a,b)] -> [a]
primeiros_map lista = mapear(fst) lista
maiusculas :: String -> String
maiusculas [] = []
maiusculas (p:r) = toUpper(p):maiusculas(r)
maiusculas_map :: String -> String
maiusculas_map lista = mapear(toUpper) lista
dobros :: Num a => [a] -> [a]
dobros [] = []
dobros (p:r) = (p * 2):dobros(r)
dobros_map :: Num a => [a] -> [a]
dobros_map lista = mapear(*2) lista
hora_em_seg :: [Float] -> [Float]
hora_em_seg [] = []
hora_em_seg (p:r) = (p * (60^2)):hora_em_seg(r)
hora_em_seg_map :: [Float] -> [Float]
hora_em_seg_map lista = mapear(*(60^2)) lista
-- 2
pares :: [Int] -> [Int]
pares [] = []
pares (p:r) = if(mod p 2 == 0) then p:pares(r) else pares(r)
pares_filter :: [Int] -> [Int]
pares_filter lista = filtrar(even) lista
alfa :: String -> String
alfa [] = []
alfa (p:r) = if(isAlpha(p)) then p:alfa(r) else alfa(r)
alfa_filter :: String -> String
alfa_filter lista = filtrar(isAlpha) lista
rm_char :: Char -> String -> String
rm_char c [] = []
rm_char c (p:r) = if(c == p) then rm_char c r else p:rm_char c r
rm_char_filter :: Char -> String -> String
rm_char_filter c lista = filtrar(/=c) lista
acima :: Int -> [Int] -> [Int]
acima x [] = []
acima x (p:r) = if(p <= x) then acima x r else p:acima x r
acima_filter :: Int -> [Int] -> [Int]
acima_filter x lista = filtrar(> x) lista
desiguais :: [(Int, Int)] -> [(Int,Int)]
desiguais [] = []
desiguais (p:r) = if(fst(p) == snd(p)) then desiguais(r) else p:desiguais(r)
desiguais_filter :: [(Int, Int)] -> [(Int,Int)]
desiguais_filter lista = filtrar(sao_iguais) lista
sao_iguais :: (Int, Int) -> Bool
sao_iguais (x, y) = x /= y
-- 3
produto :: Num a => [a] -> a
produto [] = 1
produto (p:r) = p * produto(r)
produto_foldr :: Num a => [a] -> a
produto_foldr lista = reduzir (*) 1 lista
e_logico :: [Bool] -> Bool
e_logico [] = True
e_logico (p:r) = p && e_logico(r)
e_logico_foldr :: [Bool] -> Bool
e_logico_foldr lista = reduzir (&&) True lista
concatena :: [String] -> String
concatena [] = []
concatena (p:r) = p ++ concatena(r)
concatena_foldr :: [String] -> String
concatena_foldr lista = reduzir (++) "" lista
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment