On 2006-12-19 23:46:41 -0500, dblack / wobblini.net said:

> [snip]
> Here's a slightly modified version of your Ruby version.  Part of the
> problem in yours was that you were assigning to sub_sequence but then
> throwing it away and calling combinations recursively on a different
> sub-array of sequence.

Erm, I think what you mean is that *all of my problem was that I was 
being an idiot". Yes, that seems to be it.

> 
> def combinations(sequence, n, unique=true)
>    return [] if n == 0
>    return sequence if n == 1
> 
>    result = []
> 
>    sequence.each_with_index do |e,i|
>      sub_sequence = sequence[(i + 1)..-1]
>      sub_sequence.concat(sequence[0...i]) if not unique
>      combinations(sub_sequence, n - 1, unique).each do |smaller|
>        result << [e] + [smaller]
>      end
>    end
> 
>    result
> end
> 
> 
> David

def combinations sequence, n, unique=true
  return [] if n == 0
  return sequence if n == 1
  result = []

  0.upto(sequence.length - 1) do |i|
    sub_sequence = sequence[(i + 1)..-1]
    sub_sequence += sequence[0..i] if not unique

    combinations(sub_sequence, n - 1, unique).each do |smaller|
      result << [sequence[i]] + [smaller]
    end
  end

  result
end

This works perfectly now, but yours is far prettier, so I shall use it. 
Thank you!

Best,
James