Splitting list into a list of possible tuples

Posted by user1742646 on Stack Overflow See other posts from Stack Overflow or by user1742646
Published on 2012-10-13T01:18:39Z Indexed on 2012/10/13 21:37 UTC
Read the original article Hit count: 168

Filed under:

I need to split a list into a list of all possible tuples, but I'm unsure of how to do so.

For example

pairs ["cat","dog","mouse"]

should result in

[("cat","dog"), ("cat","mouse"), ("dog","cat"), ("dog","mouse"), ("mouse","cat"), ("mouse","dog")]

I was able to form the first two, but am unsure of how to get the rest.

Here's what I have so far:

pairs :: [a] -> [(a,a)]
pairs (x:xs) = [(m,n) | m <- [x], n <- xs]

© Stack Overflow or respective owner

Related posts about haskell