Recursos-LCC

Um arquivo de todo material que consegui reunir, pertinente ao curso de LCC da UM.

View on GitHub
zipants :: [a] -> [b] -> [(a,b)]
zipants (h:t) (x:y) = (h,x) : zipants t y
zipants _ _ = []

-----------------------------------------------------

myelem ::  Eq a => a -> [a] -> Bool
myelem _ [] = False
myelem n (h:t) = if n==h then True else myelem n t

----------------------------------------------------------

replicunt ::  Int -> a -> [a]
replicunt n x = if n>0 then x:replicunt (n-1) x else []

----------------------------------------------------------

inter ::  a -> [a] -> [a]
inter _ [] = []
inter n (h:t) = h:n:inter n t

----------------------------------------------

X
agroup :: Eq a => [a] -> [[a]]
agroup []    = [[]]
agroup (h:t) = aux [h] t
              where
                aux a [] = [a]
                aux a (h:t) = if elem h a then aux (h:a) t else a :aux [h] t
X				

--------------------------------------------------------------------------------

cumcat :: [[a]] -> [a]
cumcat [] = []
cumcat (h:t) = h ++ cumcat t

--------------------------------------

X
myinits :: [a] -> [[a]]
myinits [] = [[]]
myinits l  = myinits(init l) ++ [l]
X

---------------------------------------------

tailed :: [a] -> [[a]]
tailed [] = [[]]
tailed l = [l] ++ tailed (tail l)

-----------------------------------------

isPrf ::  Eq a => [a] -> [a] -> Bool
isPrf [] l = True
isPrf l [] = False
isPrf (h:t) (x:y) = if h==x then isPrf t y else False

----------------------------------------------------------

isSuf ::  Eq a => [a] -> [a] -> Bool
isSuf [] l = True
isSuf l [] = False
isSuf x y = if (last x) == (last y) then isSuf (init x) (init y) else False

---------------------------------------------------------------------------------

isSub ::  Eq a => [a] -> [a] -> Bool
isSub [] _ = True
isSub _ [] = False
isSub (x:xs) (y:ys) = if x==y then isSub xs ys else isSub (x:xs) ys

-------------------------------------------------------------------------------

elexIn :: Eq a => a -> [a] -> [Int]
elexIn _ [] = []
elexIn n l  = aux 0 n l
                  where
                    aux _ _ [] = []
                    aux x n (h:t) | n==h = x:aux (x+1) n t
                                  |otherwise = aux (x+1) n t
								  
--------------------------------------------------------------------------

noob :: Eq a => [a] -> [a]
noob [] = []
noob (h:t) = h:noob (aux h t)
        where
            aux _ [] = []
            aux n (x:xs) = if n==x then aux n xs else x:aux n xs
			
----------------------------------------------------------------------------

del ::  Eq a => a -> [a] -> [a]
del _ [] = []
del n (h:t) | n==h = t
            | otherwise = del n t
			
--------------------------------------------------

eli ::  Eq a => [a] -> [a] -> [a]
eli [] _ = []
eli l [] = l
eli (x:xs) (y:ys) = if x==y then eli xs ys else x:eli xs (y:ys)

---------------------------------------------------------------------

une ::  Eq a => [a] -> [a] -> [a]
une [] l = l
une l [] = l
une l (h:t) | elem h l = une l t 
            | otherwise = une (l++[h]) t
			
-------------------------------------------------------------------

temRep :: Eq a => [a] -> Bool
temRep [] = False 
temRep (h:t) = elem h t || temRep t

--------------------------------------------------

algarismos :: [Char] -> [Char]
algarismos (h:t) | h=='1' = h:algarismos t
                 | h=='2' = h:algarismos t
                 | h=='3' = h:algarismos t
                 | h=='4' = h:algarismos t
                 | h=='5' = h:algarismos t
                 | h=='6' = h:algarismos t
                 | h=='7' = h:algarismos t
                 | h=='8' = h:algarismos t
                 | h=='9' = h:algarismos t
                 | h=='0' = h:algarismos t
                 | otherwise = algarismos t
				 
----------------------------------------------------------

X
posImpares :: [a] -> [a]
posImpares [] = []
posImpares l = aux 0 l
            where
                aux _ [] = []
                aux 0 (h:t) = aux 1 t
                aux 1 (h:t) = h:aux 0 t
X

--------------------------------------------------

posPares :: [a] -> [a]
posPares [] = []
posPares l = aux 0 l
            where
                aux _ [] = []
                aux 0 (h:t) = h:aux 1 t
                aux 1 (h:t) = aux 0 t
				
----------------------------------------------------

isSorted :: Ord a => [a] -> Bool
isSorted [] = True
isSorted [n] = True
isSorted (h:m:t) = if h<=m then isSorted (m:t) else False

---------------------------------------------------------------

iSort :: Ord a => [a] -> [a]
iSort [] = []
iSort (h:t) = insert h (iSort t)

insert ::  Ord a => a -> [a] -> [a]
insert x [] = [x]
insert n (x:y) | n<=x = n:insert x y
               | otherwise = x:insert n y
			   
---------------------------------------------------

menor :: String -> String -> Bool
menor [] _ = True
menor _ [] = False
menor n m = if (conta n)<(conta m) then True else False  

conta :: String -> Int
conta l = length l