I'm learning Haskell and doing the exercises from http://www.seas.upenn.edu/~cis194/spring13/lectures.html.
This is my code to validate credit cards as described on http://www.seas.upenn.edu/~cis194/spring13/hw/01-intro.pdf.
I know very little about it and welcome any advice on how to improve.
Here's my solution:
toDigits :: Integer -> [Integer]
toDigits x
| x <= 0 = []
| otherwise = toDigits (div x 10) ++ [mod x 10]
toDigitsRev :: Integer -> [Integer]
toDigitsRev x
| x <= 0 = []
| otherwise = (mod x 10) : toDigitsRev (div x 10)
doEveryTwo' :: (t -> t) -> [t] -> [t]
doEveryTwo' _ [] = []
doEveryTwo' f (x:xs) = x : doEveryTwo f xs
doEveryTwo :: (t -> t) -> [t] -> [t]
doEveryTwo _ [] = []
doEveryTwo f (x:xs) = f x : doEveryTwo' f xs
doubleEveryOther :: [Integer] -> [Integer]
doubleEveryOther [] = []
doubleEveryOther xs
| length xs `mod` 2 == 0 = doEveryTwo (*2) xs
| otherwise = head xs : doEveryTwo (*2) (tail xs)
sumDigits :: [Integer] -> Integer
sumDigits [] = 0
sumDigits (x:xs) = sum (toDigits x) + sumDigits xs
validate :: Integer -> Bool
validate x
| x < 10 = False
| otherwise = sumDigits (doubleEveryOther (toDigits x)) `mod` 10 == 0
(.)
operator yet? Otherwise I'll edit my answer. \$\endgroup\$