I'm working through a problem in the haskell wikibook and am totally stuck. They ask to "Generalize the bunny invasion example in the list monad chapter for an arbitrary number of generations."
The description of the bunny invasion example:
"It is easy to incorporate the familiar list processing functions in monadic code. Consider this example: rabbits raise an average of six kits in each litter, half of which will be female. Starting with a single mother, we can model the number of female kits in each successive generation (i.e. the number of new kits after the rabbits grow up and have their own litters):"
Prelude> let generation = replicate 3
Prelude> ["bunny"] >>= generation
["bunny","bunny","bunny"]
Prelude> ["bunny"] >>= generation >>= generation
["bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny"]
My attempt generates nested lists instead of a flat list
There are functions mentioned in the chapter which i guess I'm supposed to use including: sequence, replicate, replicateM, mapM, forM and their underscore versions which do not pass the context to the next bound monad.
["bunny"] >>= replicateM 2 gen
I get
[["bunny","bunny","bunny"],["bunny","bunny","bunny"]]
but it should be
["bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny"]