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
We have to change the type to reflect this, as well as the implementation, because we have to change `++` to `mappend`:
149
+
We have to change the type to reflect this, as well as the implementation, because we have to change `++` to `<>`:
150
150
151
151
```{.haskell:hs}
152
152
applyLog :: (Monoid m) => (a,m) -> (a -> (b,m)) -> (b,m)
153
-
applyLog (x,log) f = let (y,newLog) = f x in (y,log `mappend` newLog)
153
+
applyLog (x,log) f = let (y,newLog) = f x in (y,log <> newLog)
154
154
```
155
155
156
156
Because the accompanying value can now be any monoid value, we no longer have to think of the tuple as a value and a log, but now we can think of it as a value with an accompanying monoid value.
@@ -171,10 +171,10 @@ addDrink _ = ("beer", Sum 30)
171
171
```
172
172
173
173
We use strings to represent foods and an `Int` in a `Sum``newtype` wrapper to keep track of how many cents something costs.
174
-
Just a reminder, doing `mappend` with `Sum` results in the wrapped values getting added together:
174
+
Just a reminder, doing `<>` with `Sum` results in the wrapped values getting added together:
175
175
176
176
```{.haskell:hs}
177
-
ghci> Sum 3 `mappend` Sum 9
177
+
ghci> Sum 3 <> Sum 9
178
178
Sum {getSum = 12}
179
179
```
180
180
@@ -229,7 +229,7 @@ Its `Monad` instance is defined like so:
229
229
```{.haskell:hs}
230
230
instance (Monoid w) => Monad (Writer w) where
231
231
return x = Writer (x, mempty)
232
-
(Writer (x,v)) >>= f = let (Writer (y, v')) = f x in Writer (y, v `mappend` v')
232
+
(Writer (x,v)) >>= f = let (Writer (y, v')) = f x in Writer (y, v <> v')
233
233
```
234
234
235
235
{.right width=383 height=248}
@@ -238,15 +238,15 @@ First off, let's examine `>>=`.
238
238
Its implementation is essentially the same as `applyLog`, only now that our tuple is wrapped in the `Writer``newtype`, we have to unwrap it when pattern matching.
239
239
We take the value `x` and apply the function `f` to it.
240
240
This gives us a `Writer w a` value and we use a `let` expression to pattern match on it.
241
-
We present `y` as the new result and use `mappend` to combine the old monoid value with the new one.
241
+
We present `y` as the new result and use `<>` to combine the old monoid value with the new one.
242
242
We pack that up with the result value in a tuple and then wrap that with the `Writer` constructor so that our result is a `Writer` value instead of just an unwrapped tuple.
243
243
244
244
So, what about `return`?
245
245
It has to take a value and put it in a default minimal context that still presents that value as the result.
246
246
So what would such a context be for `Writer` values?
247
247
If we want the accompanying monoid value to affect other monoid values as little as possible, it makes sense to use `mempty`.
248
248
`mempty` is used to present identity monoid values, such as `""` and `Sum 0` and empty bytestrings.
249
-
Whenever we use `mappend` between `mempty` and some other monoid value, the result is that other monoid value.
249
+
Whenever we use `<>` between `mempty` and some other monoid value, the result is that other monoid value.
250
250
So if we use `return` to make a `Writer` value and then use `>>=` to feed that value to a function, the resulting monoid value will be only what the function returns.
251
251
Let's use `return` on the number `3` a bunch of times, only we'll pair it with a different monoid every time:
252
252
@@ -271,7 +271,7 @@ The `Writer` instance doesn't feature an implementation for `fail`, so if a patt
271
271
Now that we have a `Monad` instance, we're free to use `do` notation for `Writer` values.
272
272
It's handy for when we have a several `Writer` values and we want to do stuff with them.
273
273
Like with other monads, we can treat them as normal values and the context gets taken for us.
274
-
In this case, all the monoid values that come attached get `mappend`ed and so are reflected in the final result.
274
+
In this case, all the monoid values that come attached get `<>`ed and so are reflected in the final result.
275
275
Here's a simple example of using `do` notation with `Writer` to multiply two numbers:
276
276
277
277
```{.haskell:hs}
@@ -428,7 +428,7 @@ We just replace normal values with `Writer` values where we want and change norm
428
428
### Inefficient list construction
429
429
430
430
When using the `Writer` monad, you have to be careful which monoid to use, because using lists can sometimes turn out to be very slow.
431
-
That's because lists use `++` for `mappend` and using `++` to add something to the end of a list is slow if that list is really long.
431
+
That's because lists use `++` for `<>` and using `++` to add something to the end of a list is slow if that list is really long.
432
432
433
433
In our `gcd'` function, the logging is fast because the list appending ends up looking like this:
434
434
@@ -531,14 +531,14 @@ Here's the `Monoid` instance:
Well, we somehow have to extract the result value from the first stateful computation.
884
884
Because we're in a stateful computation right now, we can give the stateful computation `h` our current state `s`, which results in a pair of result and a new state: `(a, newState)`.
885
885
Every time so far when we were implementing `>>=`, once we had the extracted the result from the monadic value, we applied the function `f` to it to get the new monadic value.
886
-
In `Writer`, after doing that and getting the new monadic value, we still had to make sure that the context was taken care of by `mappend`ing the old monoid value with the new one.
886
+
In `Writer`, after doing that and getting the new monadic value, we still had to make sure that the context was taken care of by `<>`ing the old monoid value with the new one.
887
887
Here, we do `f a` and we get a new stateful computation `g`.
888
888
Now that we have a new stateful computation and a new state (goes by the name of `newState`) we just apply that stateful computation `g` to the `newState`.
889
889
The result is a tuple of final result and final state!
0 commit comments