Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.
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?
f as = [ (x, y) | x:xs <- as, y <- xs ]
Maybe you need the following?
import Data.List (tails) pairs :: [a] -> [(a, a)] pairs xs = [(x, y) | (x:ys) <- tails xs, y <- ys]
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?
xs
Does it generalize to more than two products, the way sequence does?
sequence
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?
Maybe you need the following?
Here's a way to make the question precise in an interesting way:
We know that
evaluates to
What we want are the elements off the diagonal, namely
What function of
xs
would do that?Does it generalize to more than two products, the way
sequence
does?