Skip to content

Instantly share code, notes, and snippets.

@btipling
Created September 29, 2015 20:21
Show Gist options
  • Save btipling/4742be1a794fec5c6a8c to your computer and use it in GitHub Desktop.
Save btipling/4742be1a794fec5c6a8c to your computer and use it in GitHub Desktop.
1st solution to csi 194 hw1 probl
main = putStrLn "Hello, World!"
doubleSecondNum :: [Int] -> [Int]
doubleSecondNum numbers
| length numbers == 0 = numbers
| otherwise = reverse (map doubleEven (zip (reverse numbers) [0..]))
doubleEven :: (Int, Int) -> Int
doubleEven pair
| (succ (snd pair)) `mod` 2 == 0 = (fst pair) * 2
| otherwise = fst pair
addListDigits :: [Int] -> Int
addListDigits ints
| length ints == 0 = 0
| otherwise = sum (map combineDigits ints)
combineDigits :: Int -> Int
combineDigits 0 = 0
combineDigits n
| n < 10 = n `mod` 10
| otherwise = (n `mod` 10) + (combineDigits (n `quot` 10))
doubleAndCombineInts :: [Int] -> Int
doubleAndCombineInts ints
| otherwise = addListDigits (doubleSecondNum ints)
isValidCCNum :: [Int] -> Bool
isValidCCNum ints
| otherwise = (doubleAndCombineInts ints) `mod` 10 == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment