-- homework for lecture 1 -- exercise 1 toDigits :: Integer -> [Integer] toDigits n | n > 0 = toDigits (n `div` 10) ++ [n `mod` 10] | otherwise = [] revList :: [Integer] -> [Integer] revList [] = [] revList (x:xs) = revList xs ++ [x] toDigitsRev :: Integer -> [Integer] toDigitsRev n = revList (toDigits n) -- exercise 2 -- [SIDE NOTE] The exercise specifies that the function below "should double -- every other number *beginning from the right*". However, it's unclear -- whether this is a requirement (i.e. the function must double digits in -- reverse order) or a mere clarification to better understand which numbers -- should be doubled and which not. Since the order doesn't matter (and doing -- the operation in reverse order would also complicate the solution), I -- decided to do it from left to right, the usual order. doubleEveryOther :: [Integer] -> [Integer] doubleEveryOther [] = [] doubleEveryOther (x:y:ys) = x*2 : (y : doubleEveryOther ys) -- exercise 3 -- This function assumes that the list only contains numbers between 0 and 19 sumDigits :: [Integer] -> Integer sumDigits [] = 0 sumDigits (x:xs) | x >= 10 = (1 + x `mod` 10) + sumDigits xs | otherwise = x + sumDigits xs -- exercise 4 validate :: Integer -> Bool validate n = sumDigits (doubleEveryOther (toDigits n)) `mod` 10 == 0 -- exercise 5 type Peg = String type Move = (Peg, Peg) hanoi :: Integer -> Peg -> Peg -> Peg -> [Move] hanoi 0 _ _ _ = [] hanoi 1 a b c = [(a,c)] hanoi 2 a b c = [(a,b), (a,c), (b,c)] hanoi n a b c = hanoi (n-1) a c b ++ [(a,c)] ++ hanoi (n-1) b a c