Created
November 30, 2014 01:47
-
-
Save sgrove/060b3a0f5970851c4ec2 to your computer and use it in GitHub Desktop.
CIS194 Ex. 1
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
module CIS194 where | |
-- Exercise 1 | |
toDigits :: Integer -> [Integer] | |
toDigits 0 = [] | |
toDigits x = map read [[c] | c <- show x, x > 0] | |
toDigitsRev :: Integer -> [Integer] | |
toDigitsRev = reverse . toDigits | |
doubleEveryOther :: [Integer] -> [Integer] | |
doubleEveryOther [] = [] | |
doubleEveryOther (x:[]) = [x] | |
doubleEveryOther (x:y:[]) = [x, y + y] | |
doubleEveryOther (x:y:ls) = [x, y + y] ++ doubleEveryOther ls | |
sumDigits :: [Integer] -> Integer | |
sumDigits xs = sum $ concatMap toDigits xs | |
validate :: Integer -> Bool | |
validate x = 0 == mod sum 10 | |
where sum = sumDigits . doubleEveryOther . toDigitsRev $ x | |
-- validate 4012888888881881 = True | |
-- validate 4012888888881882 = False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment