Questions tagged [writer-monad]
27 questions
25
votes
3 answers
Space leaks, and Writers, and Sums (oh my!)
I've been playing with the Writer Monad recently, and I've run into
what appears to be a space leak. I can't say I fully understand these
things yet, so I'd like to know what's happening here, and how to fix it.
First, here's how I can trigger this…

Adam Wagner
- 15,469
- 7
- 52
- 66
22
votes
2 answers
Is the Writer Monad effectively the same as the State Monad?
There's a great tutorial here that seems to suggest to me that the Writer Monad is basically a special case tuple object that does operations on behalf of (A,B). The writer accumulates values on the left (which is A) and that A has a…

jordan3
- 877
- 5
- 13
10
votes
1 answer
Writer implemented with Operational Monad does not work lazily
I wrote a monad with Writer functionality, using the Operational Monad approach. Then I noticed it does not work lazily.
In the code below, there is a rogueWriter that performs infinitely many statements that each write a string. The program does…

Duschvorhang
- 361
- 1
- 8
7
votes
1 answer
LYAH - Understanding comment about "tell" when chaining Writer monads
Question is in bold at the bottom.
LYAH gives this example of using the do notation with the Writer monad
import Control.Monad.Writer
logNumber :: Int -> Writer [String] Int
logNumber x = writer (x, ["number " ++ show x])
multWithLog :: Writer…

Enlico
- 23,259
- 6
- 48
- 102
4
votes
1 answer
Wrapping Maybe in WriterT to add logging
I'm totally stuck and I feel like I can use some help now, just to stay sane. I want something as simple as adding logging capabilities to a function that returns Maybe, but no matter how hard I try I just can't get the types right.
This is (as I…

SkyWriter
- 1,454
- 10
- 17
3
votes
0 answers
Can we avoid space leaks in the Writer monad by making mappend non-strict on the second parameter
I recently read about the space leak issue of the Writer/WriterT monad. If I correctly understood this problem, it is because the bind operator, i.e. (>>=) is not tail-recursive:
m >>= f = WriterT $ do
(a, w1) <- runWriterT m
(b, w2) <-…

Ruifeng Xie
- 813
- 4
- 16
3
votes
1 answer
scalaz - function composition - WriterT
Let's define a Kleisli on \/:
abstract class MyError
case class NumericalError(msg: String) extends MyError
// Either is a Monad with two type parameters: M[A,B] which represent left and right respectively
// Let's create an ad-hoc type
type…

NoIdeaHowToFixThis
- 4,484
- 2
- 34
- 69
3
votes
3 answers
Understanding example on Writer Monad
I am learning about the Writer Monad on the book Learn You A Haskell.
this is the piece of code:
import Control.Monad.Writer
logNumber :: Int -> Writer [String] Int
logNumber num = writer (num, ["Got number: " ++ show num])
multWithLog :: Writer…

lhahn
- 1,241
- 2
- 14
- 40
3
votes
1 answer
How to compose functions that return Writer[List[Int], Int]?
Suppose I have a few functions Int => Int composed with andThen:
val f1: Int => Int = _ + 1
val f2: Int => Int = _ + 2
val f3: Int => Int = _ + 3
val f = f1 andThen f2 andThen f3
Now I need to return also the intermediate results. So I can convert…

Michael
- 41,026
- 70
- 193
- 341
2
votes
1 answer
Swapping `mappend` in Writer monad
Summary: While using Writer monad, I would like to be able to switch between 2 different versions of mappend without losing the state.
I use two boolean flags to track some state:
data Flags = F Bool Bool
Now I define two Monoid instances which…

krokodil
- 1,326
- 10
- 18
2
votes
1 answer
Writer Monad Nested Twice
I am trying to nest writer monad twice using Monad Transformers. Here is a sketch:
import Control.Monad.Identity
import Control.Monad.Writer
data Struct = S Bool
instance Monoid Struct where
mempty = S True
mappend (S a) (S b) = S (a &&…

krokodil
- 1,326
- 10
- 18
2
votes
0 answers
Validating a sequence of XML elements with Writer monad
This is a follow-up to my old question.
Suppose I need to validate an XML:
I need to make sure that the root a has children a1, a2, and a3 (in this order).
I'd like to use a List (instead of scalaz.Validation) and Writer…

Michael
- 41,026
- 70
- 193
- 341
2
votes
1 answer
Memoization in the Writer monad
NOTE I'm just trying to understand what's happening in this particular piece of code shown below. I know this might not be the best way to solve the problem.
I'm trying to use the lazy Writer monad with a memozied fibonacci function to count the…

haskelline
- 1,116
- 7
- 15
1
vote
2 answers
How to fold on list of WriterT in cats
I wish to fold a list of Writer Monad from cats to one big writer without running them.
for example:
import cats.data.Writer
import cats.instances.vector._
import cats.instances.list._
import cats.instances.tuple._
import cats.Foldable
val result =…

Noam Shaish
- 1,613
- 2
- 16
- 37
1
vote
1 answer
Cats Writer type-mismatch in for expression
I've created a type
type ResultLog = Writer[List[String], Option[Double]]
My function called process wants to works on a list of Inputs and return ResultLog :
def process(inputs : List[Input]): ResultLog = {
for {
input <- inputs
…

Imran K
- 81
- 1
- 12