You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/chapter6.md
+71-30Lines changed: 71 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -125,7 +125,15 @@ No type class instance was found for
125
125
126
126
## Exercises
127
127
128
-
1. (Easy) Use the `showShape` function from the previous chapter to define a `Show` instance for the `Shape` type.
128
+
1. (Easy) Define a `Show` instance for `Point`. Match the same output as the `showPoint` function from the previous chapter.
129
+
130
+
```haskell
131
+
dataPoint
132
+
=Point
133
+
{x::Number
134
+
, y::Number
135
+
}
136
+
```
129
137
130
138
## Common Type Classes
131
139
@@ -304,18 +312,41 @@ Whatever "lifting" means in the general sense, it should be true that any reason
304
312
305
313
Many standard type classes come with their own set of similar laws. The laws given to a type class give structure to the functions of that type class and allow us to study its instances in generality. The interested reader can research the laws ascribed to the standard type classes that we have seen already.
306
314
307
-
## Exercises
315
+
### Deriving Instances
308
316
309
-
1. (Easy) The following newtype represents a complex number:
317
+
Rather than writing instances manually, you can let the compiler to most of the work for you. Take a look at this [Type Class Deriving guide](https://github.com/purescript/documentation/blob/master/guides/Type-Class-Deriving.md). That information will help you solve the following exercises.
310
318
311
-
```haskell
312
-
newtypeComplex=Complex
313
-
{real::Number
314
-
, imaginary::Number
315
-
}
316
-
```
319
+
## Exercises
320
+
321
+
The following newtype represents a complex number:
322
+
323
+
```haskell
324
+
newtypeComplex
325
+
=Complex
326
+
{real::Number
327
+
, imaginary::Number
328
+
}
329
+
```
330
+
331
+
1. (Easy) Define a `Show` instance for `Complex`.Match the output format expected by the tests (e.g. `1.2+3.4i`, `5.6-7.8i`, etc.).
332
+
333
+
2. (Easy) Derive an `Eq` instance for `Complex`. _Note_:You may instead write this instance manually, but why do more work if you don't have to?
334
+
335
+
3. (Medium) Define a `Semiring` instance for `Complex`. _Note_:You can use `wrap`and`over2` from [`Data.Newtype`](https://pursuit.purescript.org/packages/purescript-newtype/docs/Data.Newtype) to create a more concise solution.
336
+
337
+
4. (Easy) Derive (via `newtype`) a `Ring` instance for `Complex`. _Note_:You may instead write this instance manually, but that's not as convenient.
338
+
339
+
Here's the `Shape` ADT from the previous chapter:
340
+
341
+
```haskell
342
+
dataShape
343
+
=CirclePointNumber
344
+
| RectanglePointNumberNumber
345
+
| LinePointPoint
346
+
| TextPointString
347
+
```
317
348
318
-
Define `Show` and `Eq` instances for `Complex`.
349
+
5. (Medium) Derive (via `Generic`) a `Show` instance for `Shape`.How does the amount of code written and `String` output compare to `showShape` from the previous chapter? _Note_:You may instead write this instance manually, but you'll need to pay close attention to the output format expected by the tests.
319
350
320
351
## Type Class Constraints
321
352
@@ -454,35 +485,45 @@ When the program is compiled, the correct type class instance for `Show` is chos
454
485
455
486
456
487
## Exercises
457
-
1. (Easy) The following declaration defines a type of non-empty arrays of elements of type `a`:
488
+
489
+
1. (Easy) The following declaration defines a type of non-empty arrays of elements of type `a`:
1. (Difficult) Given a type constructor `f` which defines an ordered container (and so has a `Foldable` instance), we can create a new container type which includes an extra element at the front:
0 commit comments