Posted to tcl by mjanssen at Thu May 27 12:56:51 GMT 2021view raw

  1. proc combinations { list size } {
  2. if { $size == 0 } {
  3. return [list [list]]
  4. }
  5. set retval {}
  6. for { set i 0 } { ($i + $size) <= [llength $list] } { incr i } {
  7. set firstElement [lindex $list $i]
  8. set remainingElements [lrange $list [expr { $i + 1 }] end]
  9. foreach subset [combinations $remainingElements [expr { $size - 1 }]] {
  10. lappend retval [linsert $subset 0 $firstElement]
  11. }
  12. }
  13. return $retval
  14. }
  15.  
  16. set res {}
  17. for {set l 1} {$l <= [llength { a b c}]} {incr l} {
  18. lappend res {*}[combinations {a b c} $l]
  19. }
  20. set res