97 lines
2.1 KiB
Haskell
97 lines
2.1 KiB
Haskell
|
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
|