Skip to content

Commit 6e5a715

Browse files
authored
Ch6 - Clarify Action exercises (#228)
* Ch6 Clarify Action exercises * update link, remove comment
1 parent d059b81 commit 6e5a715

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

exercises/chapter6/test/Main.purs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
module Test.Main where
22

33
import Prelude
4+
import Test.MySolutions
45
import Test.NoPeeking.Solutions -- Note to reader: Delete this line
6+
57
import Data.Foldable (foldMap, foldl, foldr)
68
import Data.Hashable (hash)
79
import Data.List (List(..), (:))
@@ -166,7 +168,7 @@ Note to reader: Delete this line to expand comment block -}
166168
Assert.equal (act m1 (act m2 a))
167169
$ act (m1 <> m2) a
168170
test "Multiply Array String append concrete" do
169-
Assert.equal
171+
Assert.equal
170172
[ "foofoofoofoofoofoofoofoofoofoofoofoo"
171173
, "barbarbarbarbarbarbarbarbarbarbarbar"
172174
, "bazbazbazbazbazbazbazbazbazbazbazbaz"

exercises/chapter6/test/no-peeking/Solutions.purs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,14 @@ instance semigroupMultiply :: Semigroup Multiply where
8989
instance monoidMultiply :: Monoid Multiply where
9090
mempty = Multiply 1
9191

92-
instance actionMultiply :: Action Multiply Int where
92+
instance actionMultiplyInt :: Action Multiply Int where
9393
act (Multiply n) m = n * m
9494

95-
instance showMultiply :: Show Multiply where
96-
show (Multiply n) = "Multiply " <> show n
95+
-- These may also be written manualy
96+
derive newtype instance showMultiply :: Show Multiply
97+
derive newtype instance eqMultiply :: Eq Multiply
9798

98-
instance eqMultiply :: Eq Multiply where
99-
eq (Multiply n) (Multiply m) = n == m
100-
101-
instance repeatAction :: Action Multiply String where
99+
instance actionMultiplyString :: Action Multiply String where
102100
act (Multiply n) s = power s n
103101

104102
instance actionArray :: Action m a => Action m (Array a) where
@@ -107,23 +105,14 @@ instance actionArray :: Action m a => Action m (Array a) where
107105
newtype Self m
108106
= Self m
109107

110-
-- Why is Monoid constraint required here?
111-
-- Seems like this is already specified by Action class
112-
--instance actionSelf :: Action m (Self m) where
113108
instance actionSelf :: Monoid m => Action m (Self m) where
114109
act m1 (Self m2) = Self (m1 <> m2)
115110

116-
instance eqSelf :: Eq m => Eq (Self m) where
117-
eq (Self m1) (Self m2) = m1 == m2
118-
119-
instance showSelf :: Show m => Show (Self m) where
120-
show (Self m) = "Self " <> show m
121-
122-
instance semigroupSelf :: Semigroup m => Semigroup (Self m) where
123-
append (Self a) (Self b) = Self (a <> b)
124-
125-
instance monoidSelf :: Monoid m => Monoid (Self m) where
126-
mempty = Self mempty
111+
-- These may also be written manualy
112+
derive newtype instance showSelf :: Show m => Show (Self m)
113+
derive newtype instance eqSelf :: Eq m => Eq (Self m)
114+
derive newtype instance semigroupSelf :: Semigroup m => Semigroup (Self m)
115+
derive newtype instance monoidSelf :: Monoid m => Monoid (Self m)
127116

128117
instance repeatActionMultSelf :: Action (Self Multiply) Int where
129118
act (Self (Multiply m)) s = m * s

text/chapter6.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ Another reason to define a superclass relationship is in the case where there is
635635
## Exercises
636636

637637
1. (Medium) Define a partial function `unsafeMaximum :: Partial => Array Int -> Int` which finds the maximum of a non-empty array of integers. Test out your function in PSCi using `unsafePartial`. _Hint_: Use the `maximum` function from `Data.Foldable`.
638+
638639
1. (Medium) The `Action` class is a multi-parameter type class which defines an action of one type on another:
639640

640641
```haskell
@@ -661,20 +662,32 @@ Another reason to define a superclass relationship is in the case where there is
661662
mempty = Multiply 1
662663
```
663664

664-
This monoid acts on strings by repeating an input string some number of times. Write an instance which implements this action:
665+
Write an instance which implements this action:
666+
667+
```haskell
668+
instance actionMultiplyInt :: Action Multiply Int
669+
```
670+
Does this instance satisfy the laws listed above?
671+
672+
1. (Medium) Write an `Action` instance which repeats an input string some number of times:
665673

666674
```haskell
667-
instance repeatAction :: Action Multiply String
675+
instance actionMultiplyString :: Action Multiply String
668676
```
669-
_Hint_: Search Pursuit for a helper-function with the signature `String -> Int -> String`. Note that `String` might appear as a more generic type.
677+
_Hint_: Search Pursuit for a helper-function with the signature [`String -> Int -> String`](https://pursuit.purescript.org/search?q=String%20-%3E%20Int%20-%3E%20String). Note that `String` might appear as a more generic type (such as `Monoid`).
670678

671679
Does this instance satisfy the laws listed above?
680+
672681
1. (Medium) Write an instance `Action m a => Action m (Array a)`, where the action on arrays is defined by acting on each array element independently.
682+
673683
1. (Difficult) Given the following newtype, write an instance for `Action m (Self m)`, where the monoid `m` acts on itself using `append`:
674684

675685
```haskell
676686
newtype Self m = Self m
677687
```
688+
689+
_Note_: The testing framework requires `Show` and `Eq` instances for the `Self` and `Multiply` types. You may either write these instances manually, or let the compiler handle this for you with [`derive newtype instance`](https://github.com/purescript/documentation/blob/master/language/Type-Classes.md#derive-from-newtype) shorthand.
690+
678691
1. (Difficult) Should the arguments of the multi-parameter type class `Action` be related by some functional dependency? Why or why not? _Note_: There is no test for this exercise.
679692

680693
## A Type Class for Hashes

0 commit comments

Comments
 (0)