Last active
May 25, 2021 19:11
-
-
Save edigreat/0e88ddbfb49df63898bb to your computer and use it in GitHub Desktop.
Ejercicios Haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ------------------------------------------------------ | |
| -- Identificacion : EjerciciosHaskell.hs | |
| -- Version: : 1.0 | |
| -- Nombre del posgrado : MCC UNAM | |
| -- Instalacion : GHCi, version 7.6.3 requerido | |
| -- Fecha de Entrega : Noviembre 2014 | |
| ------------------------------------------------------- | |
| module TareaHaskell where | |
| import Data.List | |
| {- Ejercicio 1 | |
| Una funcion para sumar matrices. Las matrices | |
| pueden estar representadas como una lista de listas | |
| -} | |
| --Funcion auxiliar para sumar por renglon | |
| sumaRenglon::Num a=>[a]->[a]->[a] | |
| sumaRenglon [] [] = [] | |
| sumaRenglon (x:xs) (y:ys) | |
| | length xs == length ys = (x+y):(sumaRenglon xs ys) | |
| | otherwise = error "Error,Las Listas deben ser compatibles" | |
| --Funcion principal para sumar Matrices | |
| sumaMatrices::Num a => [[a]]->[[a]]->[[a]] | |
| sumaMatrices [] [] = [] | |
| sumaMatrices (x:xs) (y:ys) | |
| | length x == length y && length xs == length ys = (sumaRenglon x y):(sumaMatrices xs ys) | |
| | otherwise = error "Error,las filas deben ser compatibles" | |
| {- Ejercicio 2 | |
| Una Funcion que indice que dos listas son iguales | |
| aunque los elementos aparezcan en distintos Orden | |
| y/o repetidos | |
| nub quita los elementos repetidos | |
| -} | |
| --Funcion auxiliar para "Eliminar" los repetidos | |
| eliminaRepetidos::Eq a => [a] -> [a] | |
| eliminaRepetidos [] = [] | |
| eliminaRepetidos (x:xs) | |
| | x `elem` xs = eliminaRepetidos(xs) | |
| | otherwise = x:eliminaRepetidos(xs) | |
| --Funcion Principal para comparar dos listas | |
| igualdadListas::Eq a=>[a] -> [a] -> Bool | |
| igualdadListas xs ys | |
| | length (lista1\\lista2) > 0 || length(lista2\\lista1) > 0 = False | |
| | otherwise = True | |
| where | |
| lista1=eliminaRepetidos(xs) | |
| lista2=eliminaRepetidos(ys) | |
| {- Ejercicio 3 | |
| Define la funcion de invertir una lista usando la funcion | |
| de plegado por la derecha y otra por plegado por la izquierda | |
| -} | |
| --inciso a, plegado por la izquierda | |
| izqInvLista::Eq a => [a] -> [a] | |
| izqInvLista xs = foldl(\acc x -> x : acc ) [] xs | |
| --inciso b, plegado por la derecha | |
| derInvLista::Eq a => [a] -> [a] | |
| --derInvLista:: [a] -> [a] | |
| derInvLista xs = foldr(\x acc -> acc ++ [x] ) [] xs | |
| {- Ejercicio 4 | |
| Defina una funcion para ordenar dos listas que recibe como | |
| parametros | |
| -} | |
| -- Funcion auxiliar de "mezcla" de listas Ordenadas | |
| merge::Ord a => [a] -> [a] -> [a] | |
| merge [] ys = ys | |
| merge xs [] = xs | |
| merge (x:xs) (y:ys) | |
| | x<=y = x : merge (y:ys) xs | |
| | otherwise = y : merge (x:xs) ys | |
| -- Funcion auxiliar de ordenacion de una lista | |
| quickSort::Ord a => [a] -> [a] | |
| quickSort [] = [] | |
| quickSort (x:xs) = | |
| let pequenios = quickSort [ z | z<-xs ,z<=x] | |
| mayores = quickSort [ z | z<-xs ,z >x] | |
| in pequenios ++ [x] ++ mayores | |
| -- Funcion Principal , para ordenar dos Listas | |
| ordenaListas::Ord a => [a] -> [a] -> [a] | |
| ordenaListas (x:xs) (y:ys) = | |
| let primeraLista=quickSort(x:xs) | |
| segundaLista=quickSort(y:ys) | |
| in merge primeraLista segundaLista | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Muchas gracias!