Last active
November 2, 2015 20:50
-
-
Save tetek/a69c2dc4ea48c3a0f822 to your computer and use it in GitHub Desktop.
credit card validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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