Practica 1
This commit is contained in:
parent
59277fb253
commit
38ce60f8b7
7 changed files with 133 additions and 0 deletions
96
p1/Ejercicios.hs
Normal file
96
p1/Ejercicios.hs
Normal file
|
@ -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
|
9
p1/Errortipos.hs
Normal file
9
p1/Errortipos.hs
Normal file
|
@ -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
|
4
p1/Hello.hs
Normal file
4
p1/Hello.hs
Normal file
|
@ -0,0 +1,4 @@
|
|||
module Hello where
|
||||
|
||||
hello n = concat(replicate n "hello ")
|
||||
hello :: Int -> [Char]
|
5
p1/Long.hs
Normal file
5
p1/Long.hs
Normal file
|
@ -0,0 +1,5 @@
|
|||
module Long where
|
||||
|
||||
long [] = 0
|
||||
long (x:t) = 1 + long t
|
||||
|
6
p1/Power1.hs
Normal file
6
p1/Power1.hs
Normal file
|
@ -0,0 +1,6 @@
|
|||
module Power1 where
|
||||
|
||||
power1 :: Int -> Int -> Int
|
||||
|
||||
power1 _ 0 = 1
|
||||
power1 n t = n * power1 n (t - 1)
|
8
p1/Power2.hs
Normal file
8
p1/Power2.hs
Normal file
|
@ -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)
|
5
p1/Toma.hs
Normal file
5
p1/Toma.hs
Normal file
|
@ -0,0 +1,5 @@
|
|||
module Toma where
|
||||
|
||||
toma :: Int -> [a] -> [a]
|
||||
toma 0 _ = []
|
||||
toma n (x:t) = x : toma (n - 1) t
|
Loading…
Reference in a new issue