Back to Fpinscala

05.Answer

answerkey/monads/05.answer.md

latest630 B
Original Source
scala
For `List`, the `replicateM` function will generate a list of lists. It will contain all the lists of length `n` with elements selected from the input list.

For `Option`, it will generate either `Some` or `None` based on whether the input is `Some` or `None`. The `Some` case will contain a list of length `n` that repeats the element in the input `Option`.

The general meaning of `replicateM` is described well by the implementation `sequence(List.fill(n)(ma))`. It repeats the `ma` monadic value `n` times and gathers the results in a single value, where the monad `F` determines how values are actually combined.