Skip to content

Instantly share code, notes, and snippets.

@enolan
Last active March 13, 2017 23:59
Show Gist options
  • Save enolan/054bac17dde53ea0e139945c29c8ddb5 to your computer and use it in GitHub Desktop.
Save enolan/054bac17dde53ea0e139945c29c8ddb5 to your computer and use it in GitHub Desktop.
Put your homework solutions here!
If you'd like to share your solution to the homework, please paste it as a comment here and we'll look at it.
It's totally okay if you got stuck.
URL: https://git.io/vyiE8
@ryan52
Copy link

ryan52 commented Mar 13, 2017

toDigits :: Integer -> [Integer]
toDigits n = reverse (toDigitsRev n)

toDigitsRev :: Integer -> [Integer]
toDigitsRev n
         | n < 10 = [n]
         | otherwise = (n `mod` 10) : (toDigitsRev (n `div` 10))

doubleEveryOther :: [Integer] -> [Integer]
doubleEveryOther n = reverse (doubleEveryOtherReversed (reverse n))

doubleEveryOtherReversed :: [Integer] -> [Integer]
doubleEveryOtherReversed [] = []
doubleEveryOtherReversed (x:[]) = (x*2) : []
doubleEveryOtherReversed (x:y:xs) = (x) : (y*2) : (doubleEveryOtherReversed xs)

sumDigits :: [Integer] -> Integer
sumDigits xs = sum (map sum (map toDigits xs))

getMagic :: Integer -> Integer
getMagic n = sumDigits (doubleEveryOther (toDigits n))

validate :: Integer -> Bool
validate n = (getMagic n) `mod` 10 == 0

type Peg = String
type Move = (Peg, Peg)
hanoi :: Integer -> Peg -> Peg -> Peg -> [Move]
hanoi 0 _ _ _ = []
hanoi n a b c = hanoi (n-1) a c b ++ [(a, b)] ++ hanoi (n-1) c b a```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment