Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Last active August 29, 2015 14:10
import Data.Char
toDigits :: Integer -> [Integer]
toDigits n = map (\ x -> toInteger (digitToInt x)) (show n)
toDigitsRev :: Integer -> [Integer]
toDigitsRev n = reverse (toDigits n)
doubleSecond :: [Integer] -> [Integer]
doubleSecond [] = []
doubleSecond [a] = a : []
doubleSecond (b : c : xs) = b : 2 * c : doubleSecond xs
sumDigits :: [Integer] -> Integer
sumDigits [] = 0
sumDigits (x : xs) = sum (toDigits x) + sumDigits xs
isValid :: Integer -> Bool
isValid n = mod (sumDigits (doubleSecond (toDigitsRev n))) 10 == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment