[Haskell-beginners] list monad question

Matthias Guedemann matthias.guedemann at ovgu.de
Fri Oct 30 07:44:41 EDT 2009


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

                                            ___  __    __
Dipl. Inf. Matthias Guedemann              / __\/ _\  /__\
Computer Systems in Engineering           / /   \ \  /_\
Otto-von-Guericke Universitaet Magdeburg / /___ _\ \//__
Tel.: 0391 / 67-19359                    \____/ \__/\__/

More information about the Beginners mailing list