List comprehension question - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

Jan van Brügge

Is it possible to write something like this?
f as = [ (x, y) | x:xs <- as, y <- xs ]
ie a cartesian product that only has unique pairs?

Luc Tielen

Maybe you need the following?

import Data.List (tails)

pairs :: [a] -> [(a, a)]
pairs xs = [(x, y) | (x:ys) <- tails xs, y <- ys]
Kim-Ee Yeoh

Here's a way to make the question precise in an interesting way:

We know that

let xs = [1..3] in sequence [xs,xs]

evaluates to

[[1,1],[1,2],[1,3],[2,1],[2,2],[2,3],[3,1],[3,2],[3,3]]

What we want are the elements off the diagonal, namely

[[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]

What function of xs would do that?

Does it generalize to more than two products, the way sequence does?