Práctica 0 (Paradigmas)

De Cuba-Wiki
Revisión del 04:44 5 sep 2021 de Dave (discusión | contribs.) (Practica 0 - Haskell)

Ejercicio 1

Dar el tipo y describir el comportamiento de las siguientes funciones del m ́odulo Prelude de Haskell: null head tail init last take drop (++) concat (!!) elem

"null returns True if a list is empty, otherwise False"

null :: forall (t :: * -> *) a. Foldable t => t a -> Bool "head returns first elemt of list"

head :: forall a. [a] -> a "tail returns list without head"

tail :: forall a. [a] -> [a] "init returns a list withou the last item"

init :: forall a. [a] -> [a] "last returns the last items"

last :: forall a. [a] -> a "take return the first n items" take :: forall a. Int -> [a] -> [a] "drop removes the first n items"

drop :: forall a. Int -> [a] -> [a] "(++) concats lists"

(++) :: forall a. [a] -> [a] -> [a] "concat accepts a list of lists and concatenates them"

concat :: forall (t :: * -> *) a. Foldable t => t [a] -> [a] "(!!) List index (subscript) operator, starting from 0"

(!!) :: forall a. [a] -> Int -> a "returns True if the list contains an item equal to the first argument"

elem :: forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool


Ejercicio 2

Definir las siguientes funciones: a. valorAbsoluto :: Float → Float, que dado un n ́umero devuelve su valor absoluto.

b. bisiesto :: Int → Bool, que dado un n ́umero que representa un a ̃no, indica si el mismo es bisiesto.

c. factorial :: Int → Int, definida ́unicamente para enteros positivos, que computa el factorial.

d. cantDivisoresPrimos :: Int → Int, que dado un entero positivo devuelve la cantidad de divisores primos.

valorAbsoluto :: Float -> Float
valorAbsoluto x = if x < 0 then x * (-1) else x
{- Soluciones Alternativas
abs :: Int -> Int
abs n | n >= 0 = n
| otherwise = -n
myabs :: Int -> Int
myabs n = if n >= 0 then n else -n
-}
bisiesto :: Int -> Bool
bisiesto x = (mod x 4 == 0) && (mod x 100 /= 0) || (mod x 400 == 0)
{- Solucion Alternativa Mas legible
isLeapYear :: Year -> Bool
isLeapYear y = divisibleBy 400 || (divisibleBy 4 && not (divisibleBy 100))
where
divisibleBy x = mod y x == 0
-}
factorial :: Int -> Int
factorial 0 = 1
factorial x = x * factorial (x-1) 
{- Solucion Alternativa 
fac :: (Integral a) => a -> a
fac n = product [1..n]
-}
-- Creditos a https://stackoverflow.com/questions/21276844/prime-factors-in-haskell
prime_factors :: Int -> [Int]
prime_factors n =
case factors of
[] -> [n]
_ -> factors ++ prime_factors (n `div` (head factors))where factors = take 1 $ filter (\x -> (n `mod` x) == 0) [2 .. n-1]
cantDivisoresPrimos :: Int -> Int
cantDivisoresPrimos x = length (prime_factors x)


-- TESTS 
valorAbsoluto (-5) == 5
bisiesto 2024 == True
bisiesto 1 == False
bisiesto 400 == True
factorial 2 == 2
factorial 3 == 6
factorial 0 == 1 
cantDivisoresPrimos 1 == 1 
cantDivisoresPrimos 2 == 1 
cantDivisoresPrimos 10 == 2

Ejercicio 3

Contamos con los tipos Maybe y Either definidos como sigue:

data Maybe a = Nothing | Just a

data Either a b = Left a | Right b

a. Definir la función inverso :: Float → Maybe Float que dado un número devuelve su inverso multiplicativo si está definido, o Nothing en caso contrario.

b. Definir la función aEntero :: Either Int Bool → Int que convierte a entero una expresi ́on que puede ser booleana o entera. En el caso de los booleanos, el entero que corresponde es 0 para False y 1 para True.


import Data.Maybe  
inverso :: Float -> Maybe Float  
inverso x = case x of   
0 -> Nothing  
x -> Just (1/x)

fromJust (inverso 2) == 0.5
inverso 0 == Nothing


aEntero :: Either Int Bool -> Int
aEntero x = case x of 
Left x -> x
Right True -> 1 
Right False -> 0

aEntero (Right True) == 1
aEntero (Right False) == 0
aEntero (Left 5) == 5