PFH: La semana en Exercitium (del 11 al 15 de abril)
Esta semana he publicado en Exercitium las soluciones de los siguientes problemas:
- 1. Trenzado de listas
- 2. Números triangulares con n cifras distintas
- 3. Enumeración de árboles binarios
- 4. Elementos de una matriz con algún vecino menor
- 5. Reiteración de una función
A continuación se muestran las soluciones.
1. Trenzado de listas
Definir la función
1 |
trenza :: [a] -> [a] -> [a] |
tal que (trenza xs ys) es la lista obtenida intercalando los elementos de xs e ys. Por ejemplo,
1 2 3 4 |
trenza [5,1] [2,7,4] == [5,2,1,7] trenza [5,1,7] [2..] == [5,2,1,3,7,4] trenza [2..] [5,1,7] == [2,5,3,1,4,7] take 8 (trenza [2,4..] [1,5..]) == [2,1,4,5,6,9,8,13] |
Soluciones
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
import Test.QuickCheck (quickCheck) -- 1ª solución -- =========== trenza1 :: [a] -> [a] -> [a] trenza1 [] _ = [] trenza1 _ [] = [] trenza1 (x:xs) (y:ys) = x : y : trenza1 xs ys -- 2ª solución -- =========== trenza2 :: [a] -> [a] -> [a] trenza2 (x:xs) (y:ys) = x : y : trenza2 xs ys trenza2 _ _ = [] -- 3ª solución -- =========== trenza3 :: [a] -> [a] -> [a] trenza3 xs ys = concat [[x,y] | (x,y) <- zip xs ys] -- 4ª solución -- =========== trenza4 :: [a] -> [a] -> [a] trenza4 xs ys = concat (zipWith par xs ys) par :: a -> a -> [a] par x y = [x,y] -- 5ª solución -- =========== -- Explicación de eliminación de argumentos en composiciones con varios -- argumentos: f :: Int -> Int f x = x + 1 g :: Int -> Int -> Int g x y = x + y h1, h2, h3, h4, h5, h6, h7 :: Int -> Int -> Int h1 x y = f (g x y) h2 x y = f ((g x) y) h3 x y = (f . (g x)) y h4 x = f . (g x) h5 x = (f .) (g x) h6 x = ((f .) . g) x h7 = (f .) . g prop_composicion :: Int -> Int -> Bool prop_composicion x y = all (== h1 x y) [p x y | p <- [h2, h3, h4, h5, h6, h7]] -- λ> quickCheck prop_composicion -- +++ OK, passed 100 tests. -- En general, -- f . g --> \x -> f (g x) -- (f .) . g --> \x y -> f (g x y) -- ((f .) .) . g --> \x y z -> f (g x y z) -- (((f .) .) .) . g --> \w x y z -> f (g w x y z) trenza5 :: [a] -> [a] -> [a] trenza5 = (concat .) . zipWith par -- Comprobación de equivalencia -- ============================ -- La propiedad es prop_trenza :: [Int] -> [Int] -> Bool prop_trenza xs ys = all (== trenza1 xs ys) [trenza2 xs ys, trenza3 xs ys, trenza4 xs ys, trenza5 xs ys] -- La comprobación es -- λ> quickCheck prop_trenza -- +++ OK, passed 100 tests. -- Comparación de eficiencia -- ========================= -- La comparación es -- λ> last (trenza1 [1,1..] [1..4*10^6]) -- 4000000 -- (2.33 secs, 1,472,494,952 bytes) -- λ> last (trenza2 [1,1..] [1..4*10^6]) -- 4000000 -- (2.24 secs, 1,376,494,928 bytes) -- λ> last (trenza3 [1,1..] [1..4*10^6]) -- 4000000 -- (1.33 secs, 1,888,495,048 bytes) -- λ> last (trenza4 [1,1..] [1..4*10^6]) -- 4000000 -- (0.76 secs, 1,696,494,968 bytes) -- λ> last (trenza5 [1,1..] [1..4*10^6]) -- 4000000 -- (0.76 secs, 1,696,495,064 bytes) |
El código se encuentra en GitHub.
La elaboración de las soluciones se describe en el siguiente vídeo
2. Números triangulares con n cifras distintas
Los números triangulares se forman como sigue
1 2 3 4 |
* * * * * * * * * * 1 3 6 |
La sucesión de los números triangulares se obtiene sumando los números naturales. Así, los 5 primeros números triangulares son
1 2 3 4 5 |
1 = 1 3 = 1 + 2 6 = 1 + 2 + 3 10 = 1 + 2 + 3 + 4 15 = 1 + 2 + 3 + 4 + 5 |
Definir la función
1 |
triangularesConCifras :: Int -> [Integer] |
tal que (triangulares n)
es la lista de los números triangulares con n
cifras distintas. Por ejemplo,
1 2 3 4 5 |
take 6 (triangularesConCifras 1) == [1,3,6,55,66,666] take 6 (triangularesConCifras 2) == [10,15,21,28,36,45] take 6 (triangularesConCifras 3) == [105,120,136,153,190,210] take 5 (triangularesConCifras 4) == [1035,1275,1326,1378,1485] take 2 (triangularesConCifras 10) == [1062489753,1239845706] |
Soluciones
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
import Data.List (nub) import Test.QuickCheck -- 1ª solución -- =========== triangularesConCifras1 :: Int -> [Integer] triangularesConCifras1 n = [x | x <- triangulares1, nCifras x == n] -- triangulares1 es la lista de los números triangulares. Por ejemplo, -- take 10 triangulares1 == [1,3,6,10,15,21,28,36,45,55] triangulares1 :: [Integer] triangulares1 = map triangular [1..] triangular :: Integer -> Integer triangular 1 = 1 triangular n = triangular (n-1) + n -- (nCifras x) es el número de cifras distintas del número x. Por -- ejemplo, -- nCifras 325275 == 4 nCifras :: Integer -> Int nCifras = length . nub . show -- 2ª solución -- =========== triangularesConCifras2 :: Int -> [Integer] triangularesConCifras2 n = [x | x <- triangulares2, nCifras x == n] triangulares2 :: [Integer] triangulares2 = [(n*(n+1)) `div` 2 | n <- [1..]] -- 3ª solución -- =========== triangularesConCifras3 :: Int -> [Integer] triangularesConCifras3 n = [x | x <- triangulares3, nCifras x == n] triangulares3 :: [Integer] triangulares3 = 1 : [x+y | (x,y) <- zip [2..] triangulares3] -- 4ª solución -- =========== triangularesConCifras4 :: Int -> [Integer] triangularesConCifras4 n = [x | x <- triangulares4, nCifras x == n] triangulares4 :: [Integer] triangulares4 = 1 : zipWith (+) [2..] triangulares4 -- 5ª solución -- =========== triangularesConCifras5 :: Int -> [Integer] triangularesConCifras5 n = [x | x <- triangulares5, nCifras x == n] triangulares5 :: [Integer] triangulares5 = scanl (+) 1 [2..] -- Comprobación de equivalencia -- ============================ -- La 1ª propiedad es prop_triangularesConCifras1 :: Bool prop_triangularesConCifras1 = [take 2 (triangularesConCifras1 n) | n <- [1..7]] == [take 2 (triangularesConCifras2 n) | n <- [1..7]] -- La comprobación es -- λ> prop_triangularesConCifras1 -- True -- La 2ª propiedad es prop_triangularesConCifras2 :: Int -> Bool prop_triangularesConCifras2 n = all (== take 5 (triangularesConCifras2 n')) [take 5 (triangularesConCifras3 n'), take 5 (triangularesConCifras4 n'), take 5 (triangularesConCifras5 n')] where n' = 1 + n `mod` 9 -- La comprobación es -- λ> quickCheck prop_triangularesConCifras -- +++ OK, passed 100 tests. -- Comparación de eficiencia -- ========================= -- La comparación es -- λ> (triangularesConCifras1 3) !! 220 -- 5456556 -- (2.48 secs, 1,228,690,120 bytes) -- λ> (triangularesConCifras2 3) !! 220 -- 5456556 -- (0.01 secs, 4,667,288 bytes) -- -- λ> (triangularesConCifras2 3) !! 600 -- 500010500055 -- (1.76 secs, 1,659,299,872 bytes) -- λ> (triangularesConCifras3 3) !! 600 -- 500010500055 -- (1.67 secs, 1,603,298,648 bytes) -- λ> (triangularesConCifras4 3) !! 600 -- 500010500055 -- (1.20 secs, 1,507,298,248 bytes) -- λ> (triangularesConCifras5 3) !! 600 -- 500010500055 -- (1.15 secs, 1,507,298,256 bytes) |
El código se encuentra en GitHub.
La elaboración de las soluciones se describe en el siguiente vídeo
3. Enumeración de árboles binarios
Los árboles binarios se pueden representar mediante el tipo Arbol definido por
1 2 3 |
data Arbol a = H a | N (Arbol a) a (Arbol a) deriving Show |
Por ejemplo, el árbol
1 2 3 4 5 6 7 |
"B" / \ / \ / \ "B" "A" / \ / \ "A" "B" "C" "C" |
se puede definir por
1 2 |
ej1 :: Arbol String ej1 = N (N (H "A") "B" (H "B")) "B" (N (H "C") "A" (H "C")) |
Definir la función
1 |
enumeraArbol :: Arbol t -> Arbol Int |
tal que (enumeraArbol a) es el árbol obtenido numerando las hojas y los nodos de a desde la hoja izquierda hasta la raíz. Por ejemplo,
1 2 |
λ> enumeraArbol ej1 N (N (H 0) 1 (H 2)) 3 (N (H 4) 5 (H 6)) |
Gráficamente,
1 2 3 4 5 6 7 |
3 / \ / \ / \ 1 5 / \ / \ 0 2 4 6 |
Soluciones
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import Test.QuickCheck (Arbitrary, Gen, arbitrary, quickCheck, sized) import Control.Monad.State (State, evalState, get, put) data Arbol a = H a | N (Arbol a) a (Arbol a) deriving (Show, Eq) ej1 :: Arbol String ej1 = N (N (H "A") "B" (H "B")) "B" (N (H "C") "A" (H "C")) -- 1ª solución -- =========== enumeraArbol1 :: Arbol t -> Arbol Int enumeraArbol1 a = fst (aux a 0) where aux :: Arbol t -> Int -> (Arbol Int, Int) aux (H _) n = (H n, n+1) aux (N i _ d) n = (N i' n1 d', n2) where (i', n1) = aux i n (d', n2) = aux d (n1+1) -- 2ª solución -- =========== enumeraArbol2 :: Arbol t -> Arbol Int enumeraArbol2 a = evalState (aux a) 0 where aux :: Arbol t -> State Int (Arbol Int) aux (H _) = H <$> contador aux (N i _ d) = do i' <- aux i n1 <- contador d' <- aux d return (N i' n1 d') contador :: State Int Int contador = do n <- get put (n+1) return n -- 3ª solución -- =========== enumeraArbol3 :: Arbol t -> Arbol Int enumeraArbol3 a = evalState (aux a) 0 where aux :: Arbol t -> State Int (Arbol Int) aux (H _) = H <$> contador aux (N i _ d) = N <$> aux i <*> contador <*> aux d -- Comprobación de equivalencia -- ============================ -- (arbolArbitrario n) genera un árbol aleatorio de orden n. Por -- ejemplo, -- λ> generate (arbolArbitrario 3 :: Gen (Arbol Int)) -- N (N (H 19) 0 (H (-27))) 21 (N (H 2) 17 (H 26)) arbolArbitrario :: Arbitrary a => Int -> Gen (Arbol a) arbolArbitrario n | n <= 0 = H <$> arbitrary | otherwise = N <$> subarbol <*> arbitrary <*> subarbol where subarbol = arbolArbitrario (n `div` 2) -- Arbol es una subclase de Arbitrary. instance Arbitrary a => Arbitrary (Arbol a) where arbitrary = sized arbolArbitrario -- La propiedad es prop_enumeraArbol :: Arbol Int -> Bool prop_enumeraArbol a = all (== enumeraArbol1 a) [enumeraArbol2 a, enumeraArbol3 a] -- La comprobación es -- λ> quickCheck prop_enumeraArbol -- +++ OK, passed 100 tests. |
El código se encuentra en GitHub.
La elaboración de las soluciones se describe en el siguiente vídeo
4. Elementos de una matriz con algún vecino menor
Las matrices pueden representarse mediante tablas cuyos índices son pares de números naturales. Su tipo se define por
1 |
type Matriz = Array (Int,Int) Int |
Por ejemplo, la matriz
1 2 3 |
|9 4 6 5| |8 1 7 3| |4 2 5 4| |
se define por
1 2 |
ej :: Matriz ej = listArray ((1,1),(3,4)) [9,4,6,5,8,1,7,3,4,2,5,4] |
Los vecinos de un elemento son los que están a un paso en la misma fila, columna o diagonal. Por ejemplo, en la matriz anterior, el 1 tiene 8 vecinos (el 9, 4, 6, 8, 7, 4, 2 y 5) pero el 9 sólo tiene 3 vecinos (el 4, 8 y 1).
Definir la función
1 |
algunoMenor :: Matriz -> [Int] |
tal que (algunoMenor p)
es la lista de los elementos de p
que tienen algún vecino menor que él. Por ejemplo,
1 |
algunoMenor ej == [9,4,6,5,8,7,4,2,5,4] |
pues sólo el 1 y el 3 no tienen ningún vecino menor en la matriz.
Soluciones
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
import Data.Array (Array, (!), bounds, indices, inRange, listArray) import Test.QuickCheck (Arbitrary, Gen, arbitrary, chooseInt, quickCheck, vectorOf) type Matriz = Array (Int,Int) Int ej :: Matriz ej = listArray ((1,1),(3,4)) [9,4,6,5,8,1,7,3,4,2,5,4] type Pos = (Int,Int) -- 1ª solución -- =========== algunoMenor1 :: Matriz -> [Int] algunoMenor1 a = [a!p| p <- indices a, any (< a!p) (vecinos1 a p)] -- (vecinos q p) es la lista de los vecinos en la matriz a de la -- posición p. Por ejemplo, -- vecinos1 ej (2,2) == [9,4,6,8,7,4,2,5] -- vecinos1 ej (1,1) == [4,8,1] vecinos1 :: Matriz -> Pos -> [Int] vecinos1 a p = [a!p' | p' <- posicionesVecinos1 a p] -- (posicionesVecinos a p) es la lista de las posiciones de los -- vecino de p en la matriz a. Por ejemplo, -- λ> posicionesVecinos1 3 3 (2,2) -- [(1,1),(1,2),(1,3),(2,1),(2,3),(3,1),(3,2),(3,3)] -- λ> posicionesVecinos1 3 3 (1,1) -- [(1,2),(2,1),(2,2)] posicionesVecinos1 :: Matriz -> Pos -> [Pos] posicionesVecinos1 a (i,j) = [(i+di,j+dj) | (di,dj) <- [(-1,-1),(-1,0),(-1,1), ( 0,-1), ( 0,1), ( 1,-1),( 1,0),( 1,1)], inRange (bounds a) (i+di,j+dj)] -- 2ª solución -- =========== algunoMenor2 :: Matriz -> [Int] algunoMenor2 a = [a!p | p <- indices a, any (<a!p) (vecinos2 p)] where vecinos2 p = [a!p' | p' <- posicionesVecinos2 p] posicionesVecinos2 (i,j) = [(i+di,j+dj) | (di,dj) <- [(-1,-1),(-1,0),(-1,1), ( 0,-1), ( 0,1), ( 1,-1),( 1,0),( 1,1)], inRange (bounds a) (i+di,j+dj)] -- 3ª solución -- =========== algunoMenor3 :: Matriz -> [Int] algunoMenor3 a = [a!p | p <- indices a, any (<a!p) (vecinos3 p)] where vecinos3 p = [a!p' | p' <- posicionesVecinos3 p] posicionesVecinos3 (i,j) = [(i',j') | i' <- [i-1..i+1], j' <- [j-1..j+1], (i',j') /= (i,j), inRange (bounds a) (i',j')] -- 4ª solución -- =========== algunoMenor4 :: Matriz -> [Int] algunoMenor4 a = [a!p | p <- indices a, any (<a!p) (vecinos4 p)] where vecinos4 p = [a!p' | p' <- posicionesVecinos4 p] posicionesVecinos4 (i,j) = [(i',j') | i' <- [max 1 (i-1)..min m (i+1)], j' <- [max 1 (j-1)..min n (j+1)], (i',j') /= (i,j)] where (_,(m,n)) = bounds a -- 5ª solución -- =========== algunoMenor5 :: Matriz -> [Int] algunoMenor5 a = [a!p | p <- indices a, any (<a!p) (vecinos5 p)] where vecinos5 p = [a!p' | p' <- posicionesVecinos5 p] posicionesVecinos5 (i,j) = [(i-1,j-1) | i > 1, j > 1] ++ [(i-1,j) | i > 1] ++ [(i-1,j+1) | i > 1, j < n] ++ [(i,j-1) | j > 1] ++ [(i,j+1) | j < n] ++ [(i+1,j-1) | i < m, j > 1] ++ [(i+1,j) | i < m] ++ [(i+1,j+1) | i < m, j < n] where (_,(m,n)) = bounds a -- --------------------------------------------------------------------- -- Comprobación de equivalencia -- ============================ newtype Matriz2 = M Matriz deriving Show -- Generador de matrices arbitrarias. Por ejemplo, -- λ> generate matrizArbitraria -- M (array ((1,1),(3,4)) -- [((1,1),18),((1,2),6), ((1,3),-23),((1,4),-13), -- ((2,1),-2),((2,2),22),((2,3),-25),((2,4),-5), -- ((3,1),2), ((3,2),16),((3,3),-15),((3,4),7)]) matrizArbitraria :: Gen Matriz2 matrizArbitraria = do m <- chooseInt (1,10) n <- chooseInt (1,10) xs <- vectorOf (m*n) arbitrary return (M (listArray ((1,1),(m,n)) xs)) -- Matriz es una subclase de Arbitrary. instance Arbitrary Matriz2 where arbitrary = matrizArbitraria -- La propiedad es prop_algunoMenor :: Matriz2 -> Bool prop_algunoMenor (M p) = all (== algunoMenor1 p) [algunoMenor2 p, algunoMenor3 p, algunoMenor4 p, algunoMenor5 p] -- La comprobación es -- λ> quickCheck prop_algunoMenor -- +++ OK, passed 100 tests. -- Comparación de eficiencia -- ========================= -- La comparación es -- λ> maximum (algunoMenor1 (listArray ((1,1),(600,800)) [0..])) -- 479999 -- (2.20 secs, 1,350,075,240 bytes) -- λ> maximum (algunoMenor2 (listArray ((1,1),(600,800)) [0..])) -- 479999 -- (2.24 secs, 1,373,139,968 bytes) -- λ> maximum (algunoMenor3 (listArray ((1,1),(600,800)) [0..])) -- 479999 -- (2.08 secs, 1,200,734,112 bytes) -- λ> maximum (algunoMenor4 (listArray ((1,1),(600,800)) [0..])) -- 479999 -- (2.76 secs, 1,287,653,136 bytes) -- λ> maximum (algunoMenor5 (listArray ((1,1),(600,800)) [0..])) -- 479999 -- (1.67 secs, 953,937,600 bytes) |
El código se encuentra en GitHub.
La elaboración de las soluciones se describe en el siguiente vídeo
5. Reiteración de una función
Definir la función
1 |
reiteracion :: (a -> a) -> Int -> a -> a |
tal que (reiteracion f n x)
es el resultado de aplicar n
veces la función f
a x
. Por ejemplo,
1 2 3 4 |
reiteracion (+1) 10 5 == 15 reiteracion (+5) 10 0 == 50 reiteracion (*2) 4 1 == 16 reiteracion (5:) 4 [] == [5,5,5,5] |
Soluciones
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
import Test.QuickCheck (Fun (..), Positive (..), quickCheck) -- 1ª solución -- =========== reiteracion1 :: (a -> a) -> Int -> a -> a reiteracion1 _ 0 x = x reiteracion1 f n x = f (reiteracion1 f (n-1) x) -- 2ª solución -- =========== reiteracion2 :: (a -> a) -> Int -> a -> a reiteracion2 _ 0 = id reiteracion2 f n = f . reiteracion2 f (n-1) -- 3ª solución -- =========== reiteracion3 :: (a -> a) -> Int -> a -> a reiteracion3 _ 0 = id reiteracion3 f n | even n = reiteracion3 (f . f) (n `div` 2) | otherwise = f . reiteracion3 (f . f) (n `div` 2) -- 4ª solución -- =========== reiteracion4 :: (a -> a) -> Int -> a -> a reiteracion4 f n x = reiteraciones f x !! n reiteraciones :: (a -> a) -> a -> [a] reiteraciones f x = x : reiteraciones f (f x) -- 5ª solución -- =========== reiteracion5 :: (a -> a) -> Int -> a -> a reiteracion5 f n x = (iterate f x) !! n -- 6ª solución -- =========== -- Se puede eliminar los argumentos de la definición anterior como sigue: -- reiteracion4 f n x = iterate f x !! n -- reiteracion4 f n x = ((!!) (iterate f x)) n -- reiteracion4 f n x = (((!!) . (iterate f)) x) n -- reiteracion4 f n x = ((!!) . (iterate f)) x n -- reiteracion4 f n x = flip ((!!) . (iterate f)) n x -- reiteracion4 f = flip ((!!) . (iterate f)) -- reiteracion4 f = flip (((!!) .) (iterate f)) -- reiteracion4 f = flip (((!!) .) . iterate) f -- reiteracion4 f = (flip . ((!!) .) . iterate) f -- reiteracion4 = flip . ((!!) .) . iterate reiteracion6 :: (a -> a) -> Int -> a -> a reiteracion6 = flip . ((!!) .) . iterate -- Comprobación de equivalencia -- ============================ -- La propiedad es prop_reiteracion :: Fun Int Int -> Positive Int -> Int -> Bool prop_reiteracion (Fun _ f) (Positive n) x = all (== reiteracion1 f n x) [reiteracion2 f n x, reiteracion3 f n x, reiteracion4 f n x, reiteracion5 f n x, reiteracion6 f n x] -- La comprobación es -- λ> quickCheck prop_reiteracion -- +++ OK, passed 100 tests. -- Comparación de eficiencia -- ========================= -- La comparación es -- λ> reiteracion1 (+1) (10^7) 0 -- 10000000 -- (5.09 secs, 2,505,392,792 bytes) -- λ> reiteracion2 (+1) (10^7) 0 -- 10000000 -- (5.45 secs, 2,896,899,728 bytes) -- λ> reiteracion3 (+1) (10^7) 0 -- 10000000 -- (2.14 secs, 816,909,416 bytes) -- λ> reiteracion4 (+1) (10^7) 0 -- 10000000 -- (4.24 secs, 1,696,899,816 bytes) -- λ> reiteracion5 (+1) (10^7) 0 -- 10000000 -- (2.53 secs, 1,376,899,800 bytes) -- λ> reiteracion6 (+1) (10^7) 0 -- 10000000 -- (2.34 secs, 1,376,899,984 bytes) |
El código se encuentra en GitHub.
La elaboración de las soluciones se describe en el siguiente vídeo