|
| 1 | +Homework #0 |
| 2 | +=========== |
| 3 | + |
| 4 | +[Classroom](https://classroom.github.com/a/SrRMYdgl) |
| 5 | + |
| 6 | +Task 1 |
| 7 | +------ |
| 8 | + |
| 9 | +1. Create a module named `HW0.T1` and define the following type in it: |
| 10 | + |
| 11 | + ``` |
| 12 | + data a <-> b = Iso (a -> b) (b -> a) |
| 13 | +
|
| 14 | + flipIso :: (a <-> b) -> (b <-> a) |
| 15 | + flipIso (Iso f g) = Iso g f |
| 16 | +
|
| 17 | + runIso :: (a <-> b) -> (a -> b) |
| 18 | + runIso (Iso f _) = f |
| 19 | + ``` |
| 20 | + |
| 21 | +2. Implement the following functions and isomorphisms: |
| 22 | + |
| 23 | + ``` |
| 24 | + distrib :: Either a (b, c) -> (Either a b, Either a c) |
| 25 | + assocPair :: (a, (b, c)) <-> ((a, b), c) |
| 26 | + assocEither :: Either a (Either b c) <-> Either (Either a b) c |
| 27 | + ``` |
| 28 | + |
| 29 | +Task 2 |
| 30 | +------ |
| 31 | + |
| 32 | +1. Create a module named `HW0.T2` and define the following type in it: |
| 33 | + |
| 34 | + ``` |
| 35 | + type Not a = a -> Void |
| 36 | + ``` |
| 37 | + |
| 38 | +2. Implement the following functions and isomorphisms: |
| 39 | + |
| 40 | + ``` |
| 41 | + doubleNeg :: a -> Not (Not a) |
| 42 | + reduceTripleNeg :: Not (Not (Not a)) -> Not a |
| 43 | + ``` |
| 44 | + |
| 45 | +Task 3 |
| 46 | +------ |
| 47 | + |
| 48 | +1. Create a module named `HW0.T3` and define the following combinators in it: |
| 49 | + |
| 50 | + ``` |
| 51 | + s :: (a -> b -> c) -> (a -> b) -> (a -> c) |
| 52 | + s f g x = f x (g x) |
| 53 | +
|
| 54 | + k :: a -> b -> a |
| 55 | + k x y = x |
| 56 | + ``` |
| 57 | + |
| 58 | +2. Using *only those combinators* and function application (i.e. no lambdas, |
| 59 | + pattern matching, and so on) define the following additional combinators: |
| 60 | + |
| 61 | + ``` |
| 62 | + i :: a -> a |
| 63 | + compose :: (b -> c) -> (a -> b) -> (a -> c) |
| 64 | + contract :: (a -> a -> b) -> (a -> b) |
| 65 | + permute :: (a -> b -> c) -> (b -> a -> c) |
| 66 | + ``` |
| 67 | + |
| 68 | + For example: |
| 69 | + |
| 70 | + ``` |
| 71 | + i x = x -- No (parameters on the LHS disallowed) |
| 72 | + i = \x -> x -- No (lambdas disallowed) |
| 73 | + i = Prelude.id -- No (only use s and k) |
| 74 | + i = s k k -- OK |
| 75 | + i = (s k) k -- OK (parentheses for grouping allowed) |
| 76 | + ``` |
| 77 | + |
| 78 | +Task 4 |
| 79 | +------ |
| 80 | + |
| 81 | +1. Create a module named `HW0.T4`. |
| 82 | + |
| 83 | +2. Using the `fix` combinator from the `Data.Function` module define the |
| 84 | + following functions: |
| 85 | + |
| 86 | + ``` |
| 87 | + repeat' :: a -> [a] -- behaves like Data.List.repeat |
| 88 | + map' :: (a -> b) -> [a] -> [b] -- behaves like Data.List.map |
| 89 | + fib :: Natural -> Natural -- computes the n-th Fibonacci number |
| 90 | + fac :: Natural -> Natural -- computes the factorial |
| 91 | + ``` |
| 92 | + |
| 93 | + Do not use explicit recursion. For example: |
| 94 | + |
| 95 | + ``` |
| 96 | + repeat' = Data.List.repeat -- No (obviously) |
| 97 | + repeat' x = x : repeat' x -- No (explicit recursion disallowed) |
| 98 | + repeat' x = fix (x:) -- OK |
| 99 | + ``` |
| 100 | + |
| 101 | +Task 5 |
| 102 | +------ |
| 103 | + |
| 104 | +1. Create a module named `HW0.T5` and define the following type in it: |
| 105 | + |
| 106 | + ``` |
| 107 | + type Nat a = (a -> a) -> a -> a |
| 108 | + ``` |
| 109 | + |
| 110 | +2. Implement the following functions: |
| 111 | + |
| 112 | + ``` |
| 113 | + nz :: Nat a |
| 114 | + ns :: Nat a -> Nat a |
| 115 | +
|
| 116 | + nplus, nmult :: Nat a -> Nat a -> Nat a |
| 117 | +
|
| 118 | + nFromNatural :: Natural -> Nat a |
| 119 | + nToNum :: Num a => Nat a -> a |
| 120 | + ``` |
| 121 | + |
| 122 | +3. The following equations must hold: |
| 123 | + |
| 124 | + ``` |
| 125 | + nToNum nz == 0 |
| 126 | + nToNum (ns x) == 1 + nToNum x |
| 127 | +
|
| 128 | + nToNum (nplus a b) == nToNum a + nToNum b |
| 129 | + nToNum (nmult a b) == nToNum a * nToNum b |
| 130 | + ``` |
| 131 | + |
| 132 | +Task 6 |
| 133 | +------ |
| 134 | + |
| 135 | +1. Create a module named `HW0.T6` and define the following values in it: |
| 136 | + |
| 137 | + ``` |
| 138 | + a = distrib (Left ("AB" ++ "CD" ++ "EF")) -- distrib from HW0.T1 |
| 139 | + b = map isSpace "Hello, World" |
| 140 | + c = if 1 > 0 || error "X" then "Y" else "Z" |
| 141 | + ``` |
| 142 | + |
| 143 | +2. Determine the WHNF (weak head normal form) of these values: |
| 144 | + |
| 145 | + ``` |
| 146 | + a_whnf = ... |
| 147 | + b_whnf = ... |
| 148 | + c_whnf = ... |
| 149 | + ``` |
0 commit comments