Práctica 0 (pre 2010, Paradigmas)

De Cuba-Wiki

Plantilla:Back

Ejercicio 1

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

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

Ejercicio 4

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


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