Posted to tcl by mjanssen at Thu May 27 12:56:51 GMT 2021view raw
- 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
- }
- set res {}
- for {set l 1} {$l <= [llength { a b c}]} {incr l} {
- lappend res {*}[combinations {a b c} $l]
- }
- set res