Práctica 0 (pre 2010, Paradigmas)

De Cuba-Wiki
Revisión del 21:38 14 ago 2017 de 181.167.102.208 (discusión) (→‎Ejercicio 1)
(difs.) ← Revisión anterior | Revisión actual (difs.) | Revisión siguiente → (difs.)

Plantilla:Back

Ejercicio 1[editar]

a) absoluto :: Float -> Float

absoluto f | f < 0 = negate f

| otherwise = f


b) bisiesto :: Int -> Bool

bisiesto n | ((n `mod` 100) == 0) = False

| ((n `mod` 4) == 0) = True

| otherwise = False


c) factorial :: Int -> Int

factorial n | (n == 0) = 1

| (n == 1) = 1

| otherwise = n * factorial (n-1)


d)


cantDivisoresPrimos :: Int -> Int

cantDivisoresPrimos n = cantDivisoresPrimosAux n n


cantDivisoresPrimosAux :: Int -> Int -> Int

cantDivisoresPrimosAux n i | (i == 1) = 0

| ((esPrimo(i)) && ((n `mod` i)==0)) = 1 + cantDivisoresPrimosAux n (i-1)

| otherwise = cantDivisoresPrimosAux n (i-1)

esPrimo n = not (factors 2 n)


factors m n | m == n = False

| m < n = divides m n || factors (m+1) n

divides a b = (mod b a == 0)


prime 1 = False

prime 2 = True

prime n = filter (divides n) primes == []

where numbers = [x | x <- [2..n-1], x*x <= n]

primes = filter prime numbers

Ejercicio 2[editar]

inverso :: Float -> Maybe Float

inverso 0 = Nothing

inverso f = Just (1 / f)


aEntero :: (Either Int Bool) -> Int

aEntero (Left a) = a

aEntero (Right b) | b = 1

aEntero (Right b )| otherwise = 0

Ejercicio 3[editar]

Función: null

Tipo:[a] -> Bool

Devuelve True si y solo si la lista esta vacia


Función: head

Tipo:[a] -> a

Devuelve el primer elemento de la lista


Función: tail

Tipo:[a] -> a

Devuelve la lista sin el primer elemento


Función: init

Tipo:[a] -> [a]

Devuelve la lista sin el ultimo elemento


Funcion: last

Tipo: [a] -> a

Devuelve el ultimo elemento de la lista


Funcion: Take

Tipo: Int ->[a] -> [a]

Devuelve la lista formada por los k primeros elementos de la lista


Funcion: Drop

Tipo: Int ->[a] -> [a]

Devuelve la lista sin los k primeros elementos


Funcion: (++)

Tipo: [a] -> [a] -> [a]

Concatena las 2 listas


Funcion: concat

Tipo: a -> [a]

Concatena todas las listas en una


Funcion: (!!)

Tipo: [a] -> Int -> a

Devuelve el i-esimo elemento


Funcion: elem

Tipo: Eq a => a -> [a] -> Bool

Devuelve True si y solo si el elemento esta en la lista

Ejercicio 4[editar]

limpiar :: String -> String -> String limpiar (x:xs) a | null(xs) = limpiarAux [x] a | otherwise = limpiar xs (limpiarAux [x] a)

limpiarAux :: String -> String -> String limpiarAux c (a:as) | ((head c) == a) = limpiarAux c as | ((head c) /= a) = a:limpiarAux c as limpiarAux c a | (c /= a) = a


difPromedio :: [Float] -> [Float] difPromedio f = difPromedioAux f (promedio f)

difPromedioAux :: [Float] -> Float -> [Float] difPromedioAux f p = map (restarProm p) f

promedio :: [Float] -> Float promedio f = (sum f) / fromIntegral (length f)

restarProm :: Float -> Float -> Float restarProm p f = f - p

todosIguales :: [Int] -> Bool todosIguales [] = True todosIguales (i:is) = todosIgualesAux i is && todosIguales is

todosIgualesAux :: Int -> [Int] -> Bool todosIgualesAux a [] = True todosIgualesAux a (i:[]) = a == i todosIgualesAux a (i:is) = a == i && todosIgualesAux a is

Ejercicio 5[editar]


data BinTree a = Nil | Branch a (BinTree a) (BinTree a)

nullTree :: BinTree a -> Bool nullTree Nil = True nullTree _ = False

negTree :: BinTree Bool -> BinTree Bool negTree Nil = Nil negTree (Branch a b c) = Branch (not a) (negTree b) (negTree c)

prodTree :: BinTree Int -> Int prodTree (Nil) = 1 prodTree (Branch a b c) = a * prodTree b * prodTree c