Back to top.
Reduce a Dígito - Haskell

Reduce recursivamente a un dígito un entero mediante suma.

reduceADigito :: Int -> Int
reduceADigito x
    | x < 10      = x
    | otherwise   = reduceADigito $ sumaDigitos $ obtenDigitos x

sumaDigitos :: [Int] -> Int
sumaDigitos []     = 0
sumaDigitos (x:xs) = x + sumaDigitos xs

obtenDigitos :: Int -> [Int]
obtenDigitos 0 = []
obtenDigitos x = y : obtenDigitos ( ( x - y ) `div` 10 )
    where y = x `mod` 10 

Gist

http://tmblr.co/Z4bAYxS2EygO