News for hackers. Stuff that matters.
Re:Functional isn't always better by reirob on Friday October 07, 2011 @11:43AM (#3084429)
Maybe it come from the fact that you are using a non-functional language for functional stuff? In Haskell I'd do it like this:

  pairs[]     = []
  pairs(x:xs) =
     map (\ y -> (x,y)) (xs) ++ pairs xs
Tried, out in console:

  *Main> pairs "ABCD"
  [('A','B'),('A','C'),('A','D'),('B','C'),('B','D'),('C','D')]
It seems to do what is supposed to.
Re:Functional isn't always better by FalconNL on Friday October 07, 2011 @01:42PM (#3085009)
And yet another short, functional solution:

    pairs = (>>= \ ~(x:xs) -> map ((,) x) xs) . init . tails
EDIT: Or as a list comprehension:

    pairs xs = concat [map ((,) h) t | (h:t) <- tails xs]
Re:Functional isn't always better by jamwt on Friday October 07, 2011 @12:07PM (#3084577)
Haskell even has sugar for this via list comprehensions:

    Prelude> let pairs = "ABCD"
    Prelude> [(x, y) | x <- pairs, y <- pairs, x < y]
    [('A','B'),('A','C'),('A','D'),('B','C'),('B','D'),('C','D')]
Re:Functional isn't always better by dpatru on Friday October 07, 2011 @12:15PM (#3084629)
This generates the Cartesian product and filters. (x,y) where both x and y are drawn from the original list. The previous solution only generates pairs (x, y) where y is drawn from elements after x.