Created
October 25, 2011 13:05
-
-
Save benkolera/1312659 to your computer and use it in GitHub Desktop.
Luhn Checksummed Numbers
This file contains 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
import Data.Char | |
validateCC :: [Char] -> Bool | |
validateCC ccString = | |
let sum = luhnSum $ ccDigits ccString | |
in 0 == mod sum 10 | |
ccDigits digits = reverse $ map digitToInt digits | |
luhnSum digits = | |
let | |
toSumTuple = digitsToSum digits | |
toSum = fst toSumTuple ++ snd toSumTuple | |
in sum $ concatMap toDigits toSum | |
digitsToSum digits = | |
let | |
split = splitDigits digits | |
products = map (* 2) $ snd split | |
in | |
( (fst split) , products ) | |
toDigits x | x < 10 = [x] | |
| x >= 10 = [ 1 , ( x - 10 ) ] | |
splitDigits digits = (splitDigits' ([],[]) True digits) | |
where | |
splitDigits' acc _ [] = acc | |
splitDigits' (a,b) True (x:xs) = splitDigits' ( x : a , b ) False xs | |
splitDigits' (a,b) False (x:xs) = splitDigits' ( a , x:b ) True xs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment