do mapM f xs; return 1

mapM_

mapM

mapM_



main = do

mapM* putChar (replicate 10000 'a')

return ()



mapM_

mapM

mapM_

mapM

mapM_

(>>) :: Monad m => m a -> m b -> b

Monad m => m () -> m b -> m b

()

mapM

mapM

do _ <- mapM f xs; return 1

do ignore $ mapM f xs ; return 1

ignore :: Monad m => m a -> m ()

ignore . mapM f == mapM_ f

In my last post on F# I mentioned thatcaused a space leak, and that the programmer should have written. I also proposed that monadic statements should work more like in F# where non-unit return values can't be ignored. Various people seemed to misunderstand both points, so I thought I'd elaborate.First, I should clarify what I understand as a space leak. A space leak is not a memory leak in the C sense. A space leak is when a computation retains more live memory than necessary for some period of time. One sign of a possible space leak is that lots of memory is retained by garbage collection.Comparingandon the following program:Thevariant has a maximum heap residency of 2Kb, while thevariant has 226Kb. Given an input list of length, the residency ofis, whileisThe exact reasons for the space leak are quite detailed, and I'm not going to attempt to cover them. My intuition is that the return list is wrapped in the IO monad, and therefore can't be deallocated until the IO action finishes. In summary, unless you are going to use the end result of a monadic map, always useIn the above example it would be nice if the compiler had complained. You generated a value, but you didn't use it. Fortunately, it is a very easy fix - change the type of monadic bindto. Now, if a monadic statement generates a value that isn't, you get a type error. The above examples withwould be rejected by the type checker.But what if we really wanted to call? There are two options. The first is to bind the result, for example. The second option, which F# favours, is, with the auxiliary. I prefer the second option, as it clearly states that you want to ignore the result of a computation. You could even write a rule