Diferencia entre revisiones de «Práctica 0 (Paradigmas)»

De Cuba-Wiki
(Practica 0 - Haskell)
 
(Practica 0 - Haskell)
Línea 35: Línea 35:


elem :: forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
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

Revisión del 04:43 5 sep 2021

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