Você está na página 1de 16

-- Ejercicio 3.

a funcion xs z = filter (/=z) xs funcion2 xs z = length(filter (==z) xs) funcion3 xs z = (funcion xs z, funcion2 xs z) -- Ejercicio 4 noDoble :: [Char] -> [Char] noDoble (x:xs) = aux x xs where aux y [] = [y] aux y (z:zs) | y == z = aux z zs | otherwise = y: (aux z zs) {-- Ejercicio 7 Defina una funcin esta que controle si existe cierto elemento en una lista de elementos.---}

esta_a :: (Eq a) => a -> [a] -> Bool esta_a x y = (length(filter (==x) y)) /= 0 esta_b :: (Eq a) => a -> [a] -> Bool esta_b x y = (sum (convert x y)) /= 0 where convert _ [] = [] convert z (x:xs) | x == z = 1:(convert z xs) | otherwise = 0:(convert z xs) esta_c :: (Eq a) => a -> [a] -> Bool esta_c x y = or (convert x y) where convert _ [] = []

convert z (x:xs) | x == z = True: (convert z xs) | otherwise = False:(convert z xs) {-- Ejercicio 8Escriba una funcin posiciones que devuelva una lista de ndices de las posiciones de un elemento determinado en una lista de elementos.-} posiciones :: (Eq a) => a -> [a] -> [Int] posiciones x y = reverse(buscar (length y) x y) where buscar _ _ [] = [] buscar x y (z:zs) | z == y = x:(buscar (x-1) y zs)

| otherwise = buscar (x-1) y zs {-- Ejercicio 9 Escriba una funcin elimDobles, que dada una lista finita, devuelva una nueva lista, con solamente una ocurrencia de cada elemento de la lista original.-} elimDobles :: (Eq a) => [a] -> [a] elimDobles [] = [] elimDobles (x:xs) = x:elimDobles (filter (/=x) xs) {-- Ejercicio 10 Escriba una funcin dividir, que dados una lista no decreciente xs y un elemento x, devuelva una dupla de dos listas (ys,zs), con xs = ys ++ zs,

donde todos los elementos de ys sean menores o iguales que x, y todos los elementos de zs sean mayores que x.--} dividir :: (Ord a) => a -> [a] -> ([a],[a]) dividir w x = (div1 w x, div2 w x) where div1 _ [] = [] div1 w (x:xs) | x <= w = x:(div1 w xs) | otherwise = div1 w xs div2 _ [] = [] div2 w (x:xs) | x > w = x:(div2 w xs) | otherwise = div2 w xs --div1 w (x:xs) = if x <= w then x:(div1 w xs) --div2 _ [] = []

--div2 w (x:xs) = if x > w then x:(div2 w xs)} {-- Ejercicio 11 Escriba una funcin insertar, que dados una lista no decreciente ys y un elemento y, devuelva una lista no decreciente igual a ys ms el elemento e insertado en el lugar correspondiente.--} insertar :: (Ord a) => a -> [a] -> [a] insertar x [] = [x] insertar x (z:zs) = if x < z then (x:z:zs) else z: (insertar x (zs)) ----------------------------------------------concat :: [[a]] -> [a]

concat x = foldr (++) [] x desde :: (Num a) => a -> [a] desde n = n:(desde (n+1)) ----------------------------------------------{-- Ejercicio 12 Escriba una funcin unico, que dada una lista devuelva una lista que contenga exactamente los elementos que se encuentran solamente una vez en la lista dada--} unico :: (Eq a) => [a] -> [a] unico [] = [] unico (x:xs) = if (elem x xs) then unico(filter (/=x) xs) else x:(unico xs) -- Ejercicio 13 --multiplicar :: Int -> [Int] multiplicar m n = show(m*(read (filter (/=' ')

(unwords (map show n)))::Int)) --where f [x] = foldr (:[' ']) [] x {-- Ejercicio 14 un predicado getPals(L), que reciba como entrada una lista de listas de enteros ([[Int]]) y retorne una lista de listas con todas aquellas listas que sean palndromes.--} getPals :: (Eq a) => [[a]] -> [[a]] getPals [] = [] getPals (x:xs) = if f x then x:(getPals xs) else getPals xs where f x = (reverse x) == x -- Ejercicio 15.a

verif :: (Eq a) => [a] -> [a] -> Bool verif x y = or (f x y) where f [] _ = [] f x (y:ys) = if elem y x then True:(f ys x) else f ys x -- Ejercicio 15.b inter :: (Eq a) => [a] -> [a] -> [a] inter xs ys = [x | x <- xs , y <- ys, x==y] union :: (Eq a) => [a] -> [a] -> [a] union xs ys = f (xs ++ ys) where f [] = [] f (x:xs) = if elem x xs then x:(filter (/=x) xs) else x:(f xs) -- Ejercicio 15.c trans :: [[a]] -> [[a]] trans ([]:_) = []

trans x = (map head x):(trans (map tail x)) -- Ejercicio 17 esPalHor :: (Eq a) => [[a]] -> Bool esPalHor x = and(f(g x)) where f [] = [] f (x:xs) = if (reverse x) == x then True:(f xs) else False:(f xs) g ([]:_) = [] g x = (map head x):(g (map tail x)) -- Ejercicio 19 m_Sublista :: (Eq a) => [a] -> a -> [[a]] m_Sublista [] y = [] m_Sublista x y = (takeWhile (/=y) x):(m_Sublista (drop (length(takeWhile (/=y) x)+1) x) y) -- Ejercicio Quiz

conv :: (Num a) => [a] -> [a] -> [a] conv x y = if (length x) >= (length y) then (sum(zipWith (*) (take(length y) x) y)):conv (tail x) y else [] -raices :: (Floating a) => a -> a -> a -> (a,a) raices a b c = ((-b+d)/n,(-bd)/n) where d = sqrt((b*b)-4*a*c) n = 2.0*a -- Ejercicio 20 m_Matriz :: [[Int]] [[Int]] -> [[Int]] m_Matriz [] _ = [] m_Matriz (x:xs) y = (trans y)):m_Matriz where trans [] -> (mul x xs y ([]:_) =

trans x = (map head x):trans(map tail x) mul _ [] = [] mul x (y:ys) = (sum(zipWith (*) x y)):mul x ys -unionj :: (Eq a) => [a] -> [a] -> [a] unionj xs [] = xs unionj [] ys = ys unionj (x:xt) ys = if elem x ys then union xt ys else x: (union xt ys) -- Ejercicio 21 m_Lista :: [Int] -> [[Int]] m_Lista [] = [] m_Lista (x:xs) = [x,length(filter (==x) xs) +1,x*(length(filter (==x) xs) +1)]:m_Lista (filter (/=x) xs)

-- Ejercicio 22 prodCart :: (Num a) => [a] -> [a] -> [(a,a)] prodCart xs ys = [(x,y) | x <- xs , y <- ys] -- Ejercicio 23 permutaciones :: (Num a) => [a] -> [[a]] permutaciones xs = [[x,y,z] | x <- xs, y <- xs, z <- xs, x /= y, x /=z, y /= z] -- Ejercicio 24 sublistas :: (Eq a) => [[a]] -> [[a]] sublistas [] = [] sublistas (x:xs) = x: (sublistas (look x xs)) where look [] x = x look (y:ys) xs = look ys (map (filter (/=y)) xs)

-- Ejercicio 25 data Arbol = Hoja Int | Nodo Int Arbol Arbol heap :: Arbol -> Bool heap (Hoja x) = True heap (Nodo x y z) = (menor x y) && (menor x z) && (heap y) && (heap z) where menor n (Hoja x) = n <= x menor n (Nodo x _ _) = n <= x -- Ejercicios Laminas subcon :: (Eq a) => [a] -> [a] -> Bool subcon x y = all (`elem` y) x igual :: (Eq a) => [a] -> [a] -> Bool igual x y = (all (`elem` y) x) && (all (`elem` x) y) isPalHor :: [[Int]] -> Bool

isPalHor x = all (ispal) (trans x) where ispal x = (==) (reverse x) x trans ([]:_) = [] trans x = (map head x):(trans (map tail x)) -- Ejercicio parcial mydropEvery :: [a] -> Int -> [a] mydropEvery [] i = [] mydropEvery x i = (take (i-1) x) ++ (mydropEvery (drop i x) i) -- Ejercicio 18 pascal :: Int -> [[Int]] pascal 0 = [[1]] pascal n = ([1]:pascal2 n [1]) where pascal2 0 x = []

pascal2 n x = (w:(pascal2 (n-1) w)) where w = zipWith (+) (0:x) (x++[0])

Você também pode gostar