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: source_md/functors-applicative-functors-and-monoids.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1511,7 +1511,7 @@ Let's take some time and get properly acquainted with it.
1511
1511
First of all, we see that only concrete types can be made instances of `Monoid`, because the `m` in the type class definition doesn't take any type parameters.
1512
1512
This is different from `Functor` and `Applicative`, which require their instances to be type constructors which take one parameter.
1513
1513
We also see that all `Monoid`s must also be `Semigroup`s, just like all `Applicative`s must be `Functor`s.
1514
-
This is because monoids are a special kind of semigroup, as they also have an identity value.
1514
+
This is because monoids are a special kind of semigroup, as in addition to the associative operation they also have a matching identity value.
1515
1515
1516
1516
The first function is `mempty`.
1517
1517
It's not really a function, since it doesn't take parameters, so it's a polymorphic constant, kind of like `minBound` from `Bounded`.
@@ -1533,16 +1533,16 @@ The reason `mconcat` is there at all is because for some instances, there might
1533
1533
1534
1534
Before moving on to specific instances of `Monoid`, let's take a brief look at the semigroup and monoid laws.
1535
1535
We mentioned that there has to be a value that acts as the identity with respect to the binary function and that the binary function has to be associative.
1536
-
It's possible to make instances of `Semigroup` and `Monoid`that don't follow these rules, but such instances are of no use to anyone because when using the `Monoid` type class, we rely on its instances acting like monoids.
1536
+
It's possible to make instances that don't follow these rules, but such instances are of no use to anyone because when using the `Monoid` type class, we rely on its instances acting like monoids.
1537
1537
Otherwise, what's the point?
1538
1538
That's why when making instances, we have to make sure they follow these laws:
1539
1539
1540
1540
*`(x <> y) <> z = x <> (y <> z)`{.label .law} (semigroup law)
1541
1541
*`mempty <> x = x`{.label .law} (monoid law)
1542
1542
*`x <> mempty = x`{.label .law} (monoid law)
1543
1543
1544
-
The first says that `<>` has to be associative i.e. that the order in which we use `<>` to reduce several semigroup values into one doesn't matter, and the other two state that `mempty` has to act as the identity with respect to `<>`.
1545
-
Semigroups follow the first law, but monoids must follow all three.
1544
+
The first says that `<>` has to be associative i.e. that the order in which we use `<>` to reduce several values into one doesn't matter, and the other two state that `mempty` has to act as the identity with respect to `<>`.
1545
+
Semigroups follow the first law, but monoids follow all three.
1546
1546
Haskell doesn't enforce these laws, so we as the programmer have to be careful that our instances do indeed obey them.
1547
1547
1548
1548
### Lists are monoids
@@ -1903,7 +1903,7 @@ ghci> Just (Sum 3) <> Just (Sum 4)
1903
1903
Just (Sum {getSum = 7})
1904
1904
```
1905
1905
1906
-
This comes in use when you're dealing with semigroups as results of computations that may have failed.
1906
+
This comes in handy when you're dealing with semigroups as results of computations that may have failed.
1907
1907
Because of this instance, we don't have to check if the computations have failed by seeing if they're a `Nothing` or `Just` value; we can just continue to treat them as normal semigroups.
1908
1908
1909
1909
But what if the type of the contents of the `Maybe` aren't an instance of `Semigroup`?
0 commit comments