Divisibles por el primero
Enunciado
1 2 3 4 5 6 7 8 9 10 |
-- Definir la función -- divisiblesPorPrimero :: [Int] -> Bool -- tal que (divisibles xs) se verifica si todos los elementos positivos -- de xs son divisibles por el primero. Por ejemplo, -- divisiblesPorPrimero [2,6,-3,0,18,-17,10] == True -- divisiblesPorPrimero [-13] == True -- divisiblesPorPrimero [-3,6,1,-3,9,18] == False -- divisiblesPorPrimero [5,-2,-6,3] == False -- divisiblesPorPrimero [] == False -- divisiblesPorPrimero [0,2,4] == False |
Soluciones
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
-- 1ª definición (por comprensión) divisiblesPorPrimero1 :: [Int] -> Bool divisiblesPorPrimero1 [] = False divisiblesPorPrimero1 (0:_) = False divisiblesPorPrimero1 (x:xs) = and [y `rem` x == 0 | y <- xs, y > 0] -- 2ª definición (por recursión) divisiblesPorPrimero2 :: [Int] -> Bool divisiblesPorPrimero2 [] = False divisiblesPorPrimero2 (0:_) = False divisiblesPorPrimero2 (x:xs) = aux xs where aux [] = True aux (y:ys) | y > 0 = y `rem` x == 0 && aux ys | otherwise = aux ys |