When browsing Hackage, most of the monads have a Lazy and a Strict version. What is the difference exactly? Can you highlight it with some examples for the common monads (State, Reader, Writer)?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I don’t know of a separation into lazy and strict for the reader monad, the reason for the
State(T)andWriter(T)separation doesn’t apply there.The difference between the lazy and strict
WriterandStatemonads resp. their monad transformers is the implementation of the monadic bind(>>=),fmapetc. In the strict versions, the implementation pattern-matches on the pair ((result, state), resp.(result, message)), forcing its evaluation (not the evaluation of its components), while the lazy versions use an irrefutable pattern~(a,w)there, which does not force the evaluation of the pair.The lazy versions allow some applications that are not possible for the strict versions, e.g.
the
sequenceof an infinite list of actions can only start delivering its result if the(>>=)of the monad is sufficiently lazy.On the other hand, using the lazy versions often leads to the build-up of large thunks in the
(result, state)pairs, and thus to space and/or time leaks.So both variants are offered, and you can choose which suits your needs better.