Menu Close

Sumas y restas alternativas

Definir la función

   sumasYrestas :: Num a => [a] -> a

tal que (sumasYrestas xs) es el resultado de alternativamente los elementos de xs. Por ejemplo,

   sumasYrestas [3,2,4,1,7] = 3 - 2 + 4 - 1 + 7
                            = 11

Otros ejemplos,

   sumasYrestas [3,2,4]              ==  5
   sumasYrestas [3,2,4,1]            ==  4
   sumasYrestas [3,2,4,1,7]          ==  11
   sumasYrestas (replicate (10^6) 1) ==  0

Soluciones

-- 1ª definición
sumasYrestas1 :: Num a => [a] -> a
sumasYrestas1 []       = 0
sumasYrestas1 [x]      = x
sumasYrestas1 (x:y:xs) = x - y + sumasYrestas1 xs
 
-- 2ª definición
sumasYrestas2 :: Num a => [a] -> a
sumasYrestas2 xs = aux 1 xs
  where aux _ []     = 0
        aux n (y:ys) = n * y + aux (-n) ys
 
-- 3ª definición
sumasYrestas3 :: Num a => [a] -> a
sumasYrestas3 xs = auxS 0 xs
  where auxS v []       = v
        auxS v [x]      = v + x
        auxS v (x:y:xs) = auxS (v+x-y) xs
 
-- 4ª definición
sumasYrestas4 :: Num a => [a] -> a
sumasYrestas4 xs =
  sum (zipWith (*) xs [(-1)^n | n <- [0..]])
 
-- 5ª definición
sumasYrestas5 :: Num a => [a] -> a
sumasYrestas5 = sum . zipWith (*) (cycle [1,-1])
 
-- 6ª definición
sumasYrestas6 :: Num a => [a] -> a
sumasYrestas6 = foldr (-) 0
 
-- Comparación de eficiencia
--    λ> sumasYrestas1 (replicate (10^6) 1)
--    0
--    (1.50 secs, 171,623,648 bytes)
--    λ> sumasYrestas2 (replicate (10^6) 1)
--    0
--    (3.78 secs, 382,691,856 bytes)
--    λ> sumasYrestas3 (replicate (10^6) 1)
--    0
--    (1.39 secs, 204,404,024 bytes)
--    λ> sumasYrestas4 (replicate (10^6) 1)
--    0
--    (16.04 secs, 5,727,351,024 bytes)
--    λ> sumasYrestas5 (replicate (10^6) 1)
--    0
--    (1.39 secs, 235,770,344 bytes)
--    λ> sumasYrestas6 (replicate (10^6) 1)
--    0
--    (0.53 secs, 142,802,656 bytes)
Inicial

8 soluciones de “Sumas y restas alternativas

  1. enrnarbej
    sumasYrestas :: Num a => [a] -> a
    sumasYrestas (x:y:xs) = x - y + sumasYrestas xs
    sumasYrestas (x:xs)   = x
    sumasYrestas _        = 0
  2. cescarde
    sumasYrestas :: Num a => [a] -> a
    sumasYrestas []       = 0
    sumasYrestas [x]      = x
    sumasYrestas (x:y:xs) = x - y + sumasYrestas xs
  3. albcercid
    sumasYrestas :: Num a => [a] -> a
    sumasYrestas xs = auxS 0 xs
      where auxS v []       = v
            auxS v [x]      = v + x
            auxS v (x:y:xs) = auxS (v+x-y) xs
  4. joscasgom1
    sumasYrestas :: Num a => [a] -> a
    sumasYrestas xs = sum xs - 2 * sum (impares xs)
     
    impares :: Num a => [a] -> [a]
    impares xs = [y | (y,x) <- zip xs [0..], odd x]
  5. cescarde
    -- 1ª Definición
    sumasYrestasA6 :: Num a => [a] -> a
    sumasYrestasA6 []       = 0
    sumasYrestasA6 [x]      = x
    sumasYrestasA6 (x:y:xs) = x - y + sumasYrestasA6 xs
     
    -- 2ª Definición
    sumasYrestasA6' :: Num a => [a] -> a
    sumasYrestasA6' xs = sum xs - 2 * pares
      where pares   = sum [a | (a,b) <- zip xs [0..], odd b]
     
    -- 3ª Definición (esta función y la primera son prácticamente iguales)
    sumasYrestasA6'' :: Num a => [a] -> a
    sumasYrestasA6'' []       = 0
    sumasYrestasA6'' [x]      = x
    sumasYrestasA6'' (x:y:xs) = x - sumaYrestasAux (y:xs)
      where sumaYrestasAux (y:xs) = y - sumasYrestasA6'' xs
     
    -- 4ª Definición
    sumasYrestasA6''' :: Num a => [a] -> a
    sumasYrestasA6''' xs = sum (alterna xs)
      where alterna []       = []
            alterna [x]      = [x]
            alterna (x:y:xs) = (x-y) : alterna xs
  6. Chema Cortés
    sumasYrestas :: Num a => [a] -> a
    sumasYrestas = sum . zipWith (*) (cycle [1,-1])
  7. Juanjo Ortega (juaorture)
    sumasYrestas :: Num a => [a] -> a
    sumasYrestas [] = 0
    sumasYrestas (x:xs) = x + sumasYrestas' xs
     
    sumasYrestas' :: Num a => [a] -> a
    sumasYrestas' [] = 0
    sumasYrestas' (x:xs) = - x + sumasYrestas xs

Escribe tu solución

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.