I am pretty new to Haskell and I have a question. How can I write a function to return all the unique possible pairs of a list? Like: [1,2,3,4] -> [(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)]
Using a list comprehension is the easiest way.
import Data.List
pairs :: [a] -> [(a, a)]
pairs l = [(x,y) | (x:ys) <- tails l, y <- ys]
This only generates the relevant pairs so you don't have to filter out any duplicates as a separate step. tails [1,2,3,4]
generates the lists of tails [[1,2,3,4], [2,3,4], [3,4], [4], []]
and the list comprehension picks the first element from each tail and pairs it with the rest of the elements in that tail.
Looking at your example, this seems to be what you want
unique_pairs l = [(x,y) | x <- l, y <- l, x < y]
By the way, these are not just unique pairs, but unique pairs up to transposition.
You can use the nub
function:
import Data.List
uniq_pairs l = nub [(x,y) | x <-l, y<-l, x < y]
Output:
*Main> uniq_pairs [1,2,3,4]
[(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With