Combinatorial mathematics deals with, among other things, functions dealing with calculating or displaying the number of combinations one can have of 10 things taken two at a time, etc. then help me word it.
This came up on c.l.t so searching around the wiki I found some info atPower set of a list as well, but when searching, I found this page first (as I assume others would) so here is a copy stolen from there (was subsets2 on that page):
proc combinations { list size } { if { $size == 0 } { return [list [list]] } set retval {} for { set i 0 } { ($i + $size) <= [llength $list] } { incr i } { set firstElement [lindex $list $i] set remainingElements [lrange $list [expr { $i + 1 }] end] foreach subset [combinations $remainingElements [expr { $size - 1 }]] { lappend retval [linsert $subset 0 $firstElement] } } return $retval }
If order of elements is meaningfull then you would want permutations so here is a proc to do that:
proc permutations { list size } { if { $size == 0 } { return [list [list]] } set retval {} for { set i 0 } { $i < [llength $list] } { incr i } { set firstElement [lindex $list $i] set remainingElements [lreplace $list $i $i] foreach subset [subsets2 $remainingElements [expr { $size - 1 }]] { lappend retval [linsert $subset 0 $firstElement] } } return $retval }
And if you do want the power set you could use the nice version by Kevin Kenny presented on that page or make use of the combinations proc above:
proc powerset {list} { for {set i 0} {$i <= [llength $list]} {incr i} { lappend ret [combinations $list $i] } return $ret }
See also