Skip to content

Commit e6fe297

Browse files
committed
clean up a bit.
1 parent 96674c2 commit e6fe297

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/Luhn.hs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,25 @@ use it to validate the following numbers:
4242
divisibleBy10 :: Int -> Bool
4343
divisibleBy10 = (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

5454
luhn1 :: Natural -> Bool
55-
luhn1 = divisibleBy10 . sumDigits . double2nd . reverse . toDigits
55+
luhn1 = divisibleBy10 . sumUpDigits . doubleEach2nd . splitIntoDigits
5656

5757
luhn2 :: Natural -> Bool
58-
luhn2 n = divisibleBy10 (sumDigits (double2nd (reverse (toDigits n))))
58+
luhn2 n = divisibleBy10 (sumUpDigits (doubleEach2nd (splitIntoDigits n)))
5959

6060
luhn3 :: Natural -> Bool
61-
luhn3 = toDigits >>>
62-
reverse >>>
63-
double2nd >>>
64-
sumDigits >>>
61+
luhn3 = splitIntoDigits >>>
62+
doubleEach2nd >>>
63+
sumUpDigits >>>
6564
divisibleBy10
6665

6766
luhn4 :: Natural -> Bool

0 commit comments

Comments
 (0)