Diferencia entre revisiones de «Práctica 0 (pre 2010, Paradigmas)»
Sin resumen de edición |
|||
(No se muestran 5 ediciones intermedias del mismo usuario) | |||
Línea 2: | Línea 2: | ||
== Ejercicio 1 == | == 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 == | == Ejercicio 2 == | ||
inverso :: Float -> Maybe Float | |||
inverso 0 = Nothing | |||
inverso f = Just (1 / f) | |||
== Ejercicio 3 == | == Ejercicio 3 == | ||
== Ejercicio 4 == | == 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 == | == Ejercicio 5 == | ||
[[Category:Prácticas]] | [[Category:Prácticas]] | ||
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 |
Revisión del 14:26 17 mar 2009
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)
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