Skip to content

Instantly share code, notes, and snippets.

@jurisk
Last active February 27, 2020 07:52
Show Gist options
  • Select an option

  • Save jurisk/66c652ad77e3b2dde80b8b3a3f14aefb to your computer and use it in GitHub Desktop.

Select an option

Save jurisk/66c652ad77e3b2dde80b8b3a3f14aefb to your computer and use it in GitHub Desktop.
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