[Haskell-beginners] list monad question

David Virebayre dav.vire+haskell at gmail.com
Fri Oct 30 08:36:21 EDT 2009


On Fri, Oct 30, 2009 at 12:44 PM, Matthias Guedemann
<matthias.guedemann at ovgu.de> wrote:

> Now I want to make it capable to create all combinations of length n instead of
> fixed length 3 (that's why list instead of tuple), but I don't really see how.

If I understood what you ask this time, there's a function in
Control.Monad that does it :
allN = replicateM

replicateM 4 [ 1,2,3 ] = [ [ 1,1,1,1],[1,1,1,2], ....

when you write

a <- ls
b <- ls
c <- ls

You perform the monad "action" 3 times, collecting the result in a
then b, then c.
now replicateM performs a monad action n times, collecting the result in a list.
turns out that making a list of the result is exactly what you want.

David.


More information about the Beginners mailing list