@@ -42,26 +42,25 @@ use it to validate the following numbers:
4242divisibleBy10 :: Int -> Bool
4343divisibleBy10 = (0 == ) . (`mod` 10 )
4444
45- sumDigits :: [Int ] -> Int
46- sumDigits = sum . map (uncurry (+) . (`divMod` 10 )) -- map (uncurry (+) . (`divMod` 10)) [6,2,7,16,9,6,7,4,9,18,4] -> [6,2,7,7,9,6,7,4,9,9,4]
45+ sumUpDigits :: [Int ] -> Int
46+ sumUpDigits = sum . map (uncurry (+) . (`divMod` 10 )) -- map (uncurry (+) . (`divMod` 10)) [6,2,7,16,9,6,7,4,9,18,4] -> [6,2,7,7,9,6,7,4,9,9,4]
4747
48- double2nd :: [Int ] -> [Int ]
49- double2nd = zipWith (*) (cycle [1 ,2 ]) -- zipWith (*) [6,1,7,8,9,3,7,2,9,9,4] [1,2,1,2,1,2,1,2,1,2,1] -> [6,2,7,16,9,6,7,4,9,18,4]
48+ doubleEach2nd :: [Int ] -> [Int ]
49+ doubleEach2nd = zipWith (*) (cycle [1 ,2 ]) -- zipWith (*) [6,1,7,8,9,3,7,2,9,9,4] [1,2,1,2,1,2,1,2,1,2,1] -> [6,2,7,16,9,6,7,4,9,18,4]
5050
51- toDigits :: Natural -> [Int ]
52- toDigits = map digitToInt . show -- toDigits 49927398716 -> [4,9,9,2,7,3,9,8,7,1,6]
51+ splitIntoDigits :: Natural -> [Int ]
52+ splitIntoDigits = reverse . map digitToInt . show -- toDigits 49927398716 -> [4,9,9,2,7,3,9,8,7,1,6]
5353
5454luhn1 :: Natural -> Bool
55- luhn1 = divisibleBy10 . sumDigits . double2nd . reverse . toDigits
55+ luhn1 = divisibleBy10 . sumUpDigits . doubleEach2nd . splitIntoDigits
5656
5757luhn2 :: Natural -> Bool
58- luhn2 n = divisibleBy10 (sumDigits (double2nd ( reverse (toDigits n) )))
58+ luhn2 n = divisibleBy10 (sumUpDigits (doubleEach2nd (splitIntoDigits n )))
5959
6060luhn3 :: Natural -> Bool
61- luhn3 = toDigits >>>
62- reverse >>>
63- double2nd >>>
64- sumDigits >>>
61+ luhn3 = splitIntoDigits >>>
62+ doubleEach2nd >>>
63+ sumUpDigits >>>
6564 divisibleBy10
6665
6766luhn4 :: Natural -> Bool
0 commit comments