Skip to content

Instantly share code, notes, and snippets.

@thcipriani
Created July 2, 2014 23:07
Show Gist options
  • Save thcipriani/fa31d86585af37726e5b to your computer and use it in GitHub Desktop.
Save thcipriani/fa31d86585af37726e5b to your computer and use it in GitHub Desktop.
CIS194 HW1
-- Lamest answer to http://www.seas.upenn.edu/~cis194/lectures.html HW1
toDigits :: Integer -> [Integer]
toDigits x
| x <= 0 = []
| otherwise = toDigits a ++ [b]
where
a = div x 10
b = mod x 10
double2nd :: [Integer] -> [Integer]
double2nd (x:y:xs) = x : 2 * y : double2nd xs
double2nd a = a
doubleEveryOther :: [Integer] -> [Integer]
doubleEveryOther x = reverse $ double2nd $ reverse x
sumDigits :: [Integer] -> Integer
sumDigits (x:[]) = a+b
where
a = mod x 10
b = div x 10
sumDigits (x:xs) = a + b + sumDigits xs
where
a = mod x 10
b = div x 10
validate :: Integer -> Bool
validate x = mod ( sumDigits $ double2ndRev $ toDigits x ) 10 == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment