[Haskell-beginners] Re: list monad question

Ertugrul Soeylemez es at ertes.de
Fri Oct 30 10:01:29 EDT 2009


Hello Matthias,

you may want to have a look at section 11 of my monads tutorial [1],
which contains monadic library functions like replicateM together with
examples and detailed explanations.

[1] http://ertes.de/articles/monads.html#section-11


Greets,
Ertugrul.


Matthias Guedemann <matthias.guedemann at ovgu.de> wrote:

> 
> Hi,
> 
> a friend of mine wanted to write function (in Perl) that creates all tuples of
> length 3 of the elements of a given list,
> e.g. [(0,0,0),(0,0,1),(0,0,2),...,(5,5,5)] for the list [0..5]. Trying to get
> better at Haskell, I wrote a small function using the list monad for this (tuples
> replaced with lists)
> 
> all3 ls = do
>   a <- ls
>   b <- ls
>   c <- ls
>   return [a,b,c]
> 
> 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.
> As the do notation translates to 
> 
> ls >>= \a ->  etc. 
> 
> I thought it should be possible to have some sort of "foldr (>>=)" over a list
> of length n, but I can't figure out how to collect the variable number of
> results in a list for the "return".
> 
> Any hints for that?
> 
> best regards
> Matthias
> 
> 



-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://blog.ertes.de/




More information about the Beginners mailing list