-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw1.hs
85 lines (61 loc) · 1.93 KB
/
hw1.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
-- Validate a Credit Card # is legit
toDigits :: Integer -> [Integer]
toDigits x | x <=0 = []
toDigits x = toDigits (x `div` 10) ++ [x `mod` 10]
toDigitsRev :: Integer -> [Integer]
toDigitsRev x | x <=0 = []
toDigitsRev x = x `mod` 10 : toDigitsRev (x `div` 10)
doubleEveryOther :: [Integer] -> [Integer]
doubleEveryOther [] = []
doubleEveryOther (x:[]) = [x]
doubleEveryOther (x:y:zs) = x * 2: y : doubleEveryOther zs
sumDigits :: [Integer] -> Integer
sumDigits [] = 0
sumDigits (x:[]) = x
sumDigits (x:y) = sumDigits (toDigits x) + sumDigits y
validate :: Integer -> Bool
validate x = ( sumDigits (doubleEveryOther (toDigits x) ) ) `mod` 10 == 0
-- Hanoi Peg Mover
type Peg = String
type Move = (Peg, Peg)
-- hanoi :: Integer -> Peg -> Peg -> Peg -> [Move]
data Thing = Shoe
| Ship
| SealingWax
| Cabbage
| King
deriving Show
data FailableDouble = Failure
| OK Double
deriving Show
safeDiv :: Double -> Double -> FailableDouble
safeDiv _ 0 = Failure
safeDiv x y = OK ( x / y )
failureToZero :: FailableDouble -> Double
failureToZero Failure = 0
failureToZero (OK d) = d
lastButOne xs = head $ (drop (length xs-2) xs)
-- Algebraic Data Types
data List a = Cons a (List a)
| Nil
deriving (Show)
fromList (x:xs) = Cons x (fromList xs)
fromList [] = Nil
-- Write the inverse of fromList; take `List a` and generate `[a]`
-- Cons 'd' (Cons 'u' (Cons 'r' (Cons 'i' (Cons 'a' (Cons 'n' Nil)))))
fromList' (Cons x xs) = x : fromList' xs
fromList' Nil = []
-- Define a tree type that has only one constructor, like our Java example.
-- Instead of the Empty constructor, use the Maybe type to refer to a node's children
-- class Example
-- {
-- static Tree<String> simpleTree()
-- {
-- return new Tree<String>(
-- "parent",
-- new Tree<String>("left leaf", null, null),
-- new Tree<String>("right leaf", null, null));
-- }
-- }
data Tree a = Node a (Tree a) (Tree a)
| Maybe