-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomework1.hs
56 lines (39 loc) · 1.42 KB
/
homework1.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{-# OPTIONS_GHC -Wall #-}
----------------------------------------
-- Exercise 1
----------------------------------------
toDigits :: Integer -> [Integer]
toDigitsRev :: Integer -> [Integer]
toDigitsRev n
| n < 1 = []
| n < 10 = [n]
| otherwise = (n `mod` 10) : (toDigitsRev (n `div` 10))
toDigits = reverse . toDigitsRev
----------------------------------------
-- Exercise 2
----------------------------------------
doubleEveryOther :: [Integer] -> [Integer]
doubleEveryOther' :: [Integer] -> [Integer]
doubleEveryOther' [] = []
doubleEveryOther' (x:[]) = [x]
doubleEveryOther' (x:(y:zs)) = x : (2*y) : doubleEveryOther'(zs)
doubleEveryOther = reverse . doubleEveryOther' . reverse
----------------------------------------
-- Exercise 3
----------------------------------------
sumDigits :: [Integer] -> Integer
sumDigits xs = foldl (+) 0 (map (\x -> foldl (+) 0 (toDigits x)) xs)
----------------------------------------
-- Exercise 4
----------------------------------------
validate :: Integer -> Bool
validate n = ((sumDigits (doubleEveryOther (toDigits n))) `rem` 10) == 0
-- main = putStrLn (show (validate 4012888888881881))
----------------------------------------
-- Exercise 5
----------------------------------------
type Peg = String
type Move = (Peg, Peg)
hanoi :: Integer -> Peg -> Peg -> Peg -> [Move]
hanoi 1 a b _ = [(a,b)]
hanoi n a b c = concat [(hanoi (n-1) a c b), [(a,b)], (hanoi (n-1) c b a)]