Last active
February 27, 2020 07:52
-
-
Save jurisk/66c652ad77e3b2dde80b8b3a3f14aefb to your computer and use it in GitHub Desktop.
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
| doubleOddElements :: [Int] -> [Int] | |
| doubleOddElements [] = [] | |
| doubleOddElements (x: []) = [x] | |
| doubleOddElements (x0 : x1 : xs) = (x0 : x1 * 2 : doubleOddElements xs) | |
| digits :: Integral x => x -> [x] | |
| digits 0 = [] | |
| digits x = digits (x `div` 10) ++ [x `mod` 10] | |
| isLuhn :: Int -> Bool | |
| isLuhn x = s `mod` 10 == 0 | |
| where | |
| d = digits x | |
| dd = doubleOddElements d | |
| sd = fmap (\x -> if (x > 9) then (x - 9) else x) dd | |
| s = sum sd | |
| main :: IO () | |
| main = do | |
| input <- getLine | |
| let x = read input :: Int | |
| let result = isLuhn x | |
| putStrLn $ (input ++ " is " ++ (if (result) then "" else "NOT ") ++ "a Luhn number") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment