[Haskell-beginners] Need help understanding the tell function in the Monad Writer example in LYAH
Olumide
50295 at web.de
Fri Feb 3 11:31:32 UTC 2017
Hello List,
Would someone kindly explain how the tell function interacts with the
Writer Monad as show below:
(Example taken from chapter 14 of LYAH
http://learnyouahaskell.com/for-a-few-monads-more#reader )
multWithLog :: Writer [String] Int
multWithLog = do
a <- logNumber 3
b <- logNumber 5
tell ["Gonna multiply these two"]
return (a*b)
Result:
ghci> runWriter multWithLog
(15,["Got number: 3","Got number: 5","Gonna multiply these two"])
I know that tell function binds to an argument that is discarded but I
don't know how its argument "Gonna multiply these two" is concatenated
with the other the Writer created by logNumber 3 and logNumber 5.
Also, I don't understand the paragraph following the example:
"It's important that return (a*b) is the last line, because the result
of the last line in a do expression is the result of the whole do
expression. Had we put tell as the last line, () would have been the
result of this do expression. We'd lose the result of the
multiplication. However, the log would be the same."
Regards,
- Olumide
More information about the Beginners
mailing list