From 38ce60f8b788adb9d07f60d74a5b90a499d7c5dd Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Mon, 15 Oct 2018 19:13:06 +0200 Subject: [PATCH] Practica 1 --- p1/Ejercicios.hs | 96 ++++++++++++++++++++++++++++++++++++++++++++++++ p1/Errortipos.hs | 9 +++++ p1/Hello.hs | 4 ++ p1/Long.hs | 5 +++ p1/Power1.hs | 6 +++ p1/Power2.hs | 8 ++++ p1/Toma.hs | 5 +++ 7 files changed, 133 insertions(+) create mode 100644 p1/Ejercicios.hs create mode 100644 p1/Errortipos.hs create mode 100644 p1/Hello.hs create mode 100644 p1/Long.hs create mode 100644 p1/Power1.hs create mode 100644 p1/Power2.hs create mode 100644 p1/Toma.hs diff --git a/p1/Ejercicios.hs b/p1/Ejercicios.hs new file mode 100644 index 0000000..5b64687 --- /dev/null +++ b/p1/Ejercicios.hs @@ -0,0 +1,96 @@ +module Ejercicios where +import Data.Char + +-- EJERCICIOS SOBRE TIPOS BASICOS + +-- Ejercicio 1 +siguienteLetra :: Char -> Char +siguienteLetra 'z' = 'a' +siguienteLetra 'Z' = 'A' +siguienteLetra a = chr ((ord a) + 1) + +-- Ejercicio 2 +sumatorio :: Int -> Int -> Int +sumatorio a b + | a == b = a + | otherwise = a + (sumatorio (a + 1) b) + +-- Ejercicio 3 +productorio :: Int -> Int -> Int +productorio a b + | a == b = a + | otherwise = a * (productorio (a + 1) b) + +-- Ejercicio 4 +maximo :: Int -> Int -> Int +maximo a b + | signum (a - b) == 1 = a + | otherwise = b + +-- Ejercicio 5 +fact :: Int -> Int +fact 0 = 1 +fact n = n * fact (n - 1) + +-- Ejercicio 6 +sumaFacts :: Int -> Int +sumaFacts 0 = fact 0 +sumaFacts n = fact n + sumaFacts (n - 1) + +-- EJERCICIOS SOBRE TIPOS LISTAS + +-- Ejercicio 1 +sumaPares :: [(Int, Int)] -> [Int] +sumaPares [] = [] +sumaPares (x:xs) = fst x + snd x : sumaPares xs +-- Alternativa sumaPares ((x, y):xs) = x+y : sumaPares xs + +-- Ejercicio 2 +cab :: [a] -> a +cab (x:xs) = x +cola :: [a] -> a +cola (x:xs) + | xs == [] = x + | otherwise = cola xs + +-- Ejercicio 3 +divisores :: Int -> [Int] +divisores n = [i | i <- [1..n], mod n i == 0] + +-- Ejercicio 4 +enLista :: Int -> [Int] -> Bool +enLista n [] = False +enLista n (x:xs) + | n == x = True + | otherwise = enLista n xs + +-- Ejercicio 5 +replace :: [Int] -> Int -> Int -> [Int] +replace [] n p = [] +replace (x:xs) n p + | x == n = p : replace xs n p + | otherwise = x : replace xs n p + +-- Ejercicio 6 +count :: Int -> [Int] -> Int +count n [] = 0 +count n (x:xs) + | n == x = 1 + count n xs + | otherwise = count n xs + +-- Ejercicio 7 +multiplosCinco :: [Int] +multiplosCinco = [x * 5 | x <- [1..]] -- Listas intensionales multiplicando +multiplosCinco4 :: [Int] +multiplosCinco4 = [x | x <- [1..], mod x 5 == 0] -- Listas intensionales comprobando +multiplosCinco2 :: [Int] +multiplosCinco2 = map (* 5) [1..] -- Orden superior (map) +multiplosCinco3 :: [Int] +multiplosCinco3 = filter (\x -> mod x 5 == 0) [1..] -- Orden superior (filter) + +-- Ejercicio 8 +cogeMayus :: [Char] -> [Char] +cogeMayus [] = [] +cogeMayus (x:xs) + | isUpper x = x : cogeMayus xs + | otherwise = cogeMayus xs diff --git a/p1/Errortipos.hs b/p1/Errortipos.hs new file mode 100644 index 0000000..4eeed24 --- /dev/null +++ b/p1/Errortipos.hs @@ -0,0 +1,9 @@ +module Errortipos where + +-- Causa error de tipos +-- main = f (0, 'a') +-- Expresion corregida +main = f('a', 0) + +f :: (Char, Int) -> String +f (c, i) = [c] ++ show i diff --git a/p1/Hello.hs b/p1/Hello.hs new file mode 100644 index 0000000..3f0e774 --- /dev/null +++ b/p1/Hello.hs @@ -0,0 +1,4 @@ +module Hello where + +hello n = concat(replicate n "hello ") +hello :: Int -> [Char] diff --git a/p1/Long.hs b/p1/Long.hs new file mode 100644 index 0000000..709c9ad --- /dev/null +++ b/p1/Long.hs @@ -0,0 +1,5 @@ +module Long where + +long [] = 0 +long (x:t) = 1 + long t + diff --git a/p1/Power1.hs b/p1/Power1.hs new file mode 100644 index 0000000..81b9d7a --- /dev/null +++ b/p1/Power1.hs @@ -0,0 +1,6 @@ +module Power1 where + +power1 :: Int -> Int -> Int + +power1 _ 0 = 1 +power1 n t = n * power1 n (t - 1) diff --git a/p1/Power2.hs b/p1/Power2.hs new file mode 100644 index 0000000..f224c99 --- /dev/null +++ b/p1/Power2.hs @@ -0,0 +1,8 @@ +module Power1 where + +power2 :: Int -> Int -> Int + +power1 _ 0 = 1 +power1 n t + | even t = power2 (n * n) (div t 2) + | otherwise = n * power2 (n * n) (div t 2) diff --git a/p1/Toma.hs b/p1/Toma.hs new file mode 100644 index 0000000..2b038ec --- /dev/null +++ b/p1/Toma.hs @@ -0,0 +1,5 @@ +module Toma where + +toma :: Int -> [a] -> [a] +toma 0 _ = [] +toma n (x:t) = x : toma (n - 1) t