Skip to content

Commit 7eaf36d

Browse files
authored
Ch7 derived solutions (#235)
* Ch7 point to generic derive guide and include manual solutions too * Rely on derive content from previous chapter
1 parent ccd597b commit 7eaf36d

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,31 @@ data Tree a
7575
= Leaf
7676
| Branch (Tree a) a (Tree a)
7777

78-
-- These may alternatively be written by hand, rather than derived from generic.
78+
-- Solution using derived instances:
79+
80+
derive instance eqTree :: Eq a => Eq (Tree a)
81+
7982
derive instance genericTree :: Generic (Tree a) _
8083

84+
instance showTree :: Show a => Show (Tree a) where
85+
show t = genericShow t
86+
87+
{-
88+
-- Solution using manually-written instances:
89+
8190
instance eqTree :: Eq a => Eq (Tree a) where
82-
eq t = genericEq t
91+
eq Leaf Leaf = true
92+
eq (Branch t1a va t2a) (Branch t1b vb t2b)
93+
= t1a == t1b
94+
&& va == vb
95+
&& t2a == t2b
96+
eq _ _ = false
8397
8498
instance showTree :: Show a => Show (Tree a) where
85-
show t = genericShow t
99+
show Leaf = "Leaf"
100+
show (Branch t1 v t2) =
101+
"(Branch " <> show t1 <> " " <> show v <> " " <> show t2 <> ")"
102+
-}
86103

87104
-- Exercise 2
88105
instance functorTree :: Functor Tree where

text/chapter7.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,14 @@ Other traversable functors include `Array`, and `Tuple a` and `Either a` for any
645645
data Tree a = Leaf | Branch (Tree a) a (Tree a)
646646
```
647647

648+
Recall from the previous chapter that you may either write these instances manually or let the compiler derive them for you.
649+
650+
There are many "correct" formatting options for `Show` output. The test for this exercise expects the following whitespace style. This happens to match the default formatting of generic show, so you only need to make note of this if you're planning on writing this instance manually.
651+
652+
```
653+
(Branch (Branch Leaf 8 Leaf) 42 Leaf)
654+
```
655+
648656
1. (Medium) Write a `Traversable` instance for `Tree a`, which combines side-effects from left-to-right. _Hint_: There are some additional instance dependencies that need to be defined for `Traversable`.
649657

650658
1. (Medium) Write a function `traversePreOrder :: forall a m b. Applicative m => (a -> m b) -> Tree a -> m (Tree b)` that performs a pre-order traversal of the tree. This means the order of effect execution is root-left-right, instead of left-root-right as was done for the previous in-order traverse exercise. _Hint_: No additional instances need to be defined, and you don't need to call any of the the functions defined earlier. Applicative do notation (`ado`) is the easiest way to write this function.

0 commit comments

Comments
 (0)