Skip to content

Instantly share code, notes, and snippets.

@tetek
Last active November 2, 2015 20:50
Show Gist options
  • Select an option

  • Save tetek/a69c2dc4ea48c3a0f822 to your computer and use it in GitHub Desktop.

Select an option

Save tetek/a69c2dc4ea48c3a0f822 to your computer and use it in GitHub Desktop.
credit card validation
toDigits :: Integer -> [Integer]
toDigits 0 = []
toDigits a = (toDigits c) ++ [b] where (c,b) = divMod a 10
toDigitsRev = reverse . toDigits
doubleEveryOther :: [Integer] -> [Integer]
doubleEveryOther (x:y:xs) = x*2 : y : (doubleEveryOther xs)
doubleEveryOther a = a
sumDigits :: [Integer] -> Integer
sumDigits = sum . concat . map toDigits
validate :: Integer -> Bool
validate a = (==) 0 (mod (g a) 10 ) where g = sumDigits . doubleEveryOther . toDigits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment