-
Notifications
You must be signed in to change notification settings - Fork 1
/
lecture-1.hs
35 lines (27 loc) · 1.08 KB
/
lecture-1.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
sumtorial :: Integer -> Integer
sumtorial 0 = 0
sumtorial n = n + sumtorial (n-1)
toDigits :: Integer -> [Integer]
toDigits n
| n < 1 = []
| otherwise = toDigits (n `div` 10) ++ [n `mod` 10]
toDigitsRev :: Integer -> [Integer]
toDigitsRev = reverse . toDigits
doubleFromLeft :: [Integer] -> [Integer]
doubleFromLeft [] = []
doubleFromLeft (x:[]) = [x]
doubleFromLeft (x:y:zs) = x : 2*y : doubleFromLeft(zs)
doubleEveryOther :: [Integer] -> [Integer]
doubleEveryOther = reverse . doubleFromLeft . reverse
sumDigits :: [Integer] -> Integer
sumDigits = sum . concat . map toDigits
validate :: Integer -> Bool
validate n
| (sumDigits . doubleEveryOther . toDigits) n `mod` 10 == 0 = True
| otherwise = False
type Peg = String
type Move = (Peg, Peg)
hanoi :: Integer -> Peg -> Peg -> Peg -> [Move]
hanoi 0 _ _ _ = [ ]
hanoi 1 a b c = [(a, b)]
hanoi n a b c = hanoi (n - 1) a c b ++ [(a, b)] ++ hanoi (n-1) c b a