Version 2 of Set Functions

Updated 2003-07-03 10:40:15

Mathematical set based functions

Of obvious enough kind, but important, and underused, I guess. Page started by Theo Verelst

These are the first definitions:

 proc set:empty {}    {return {}}
 proc set:add {a b}   {return [set:union $a $b]}
 proc set:isempty {s} {expr {[llength $s] == 0}}
 proc set:size {s}    {return [llength $s]}
 proc set:union {a b} {
    set o {}
    foreach i $a {
       if {[lsearch $b $i] < 0} {lappend o $i}
    }
    set o [concat $o $b]
    return $o
 }
 proc set:intersection {a b} {
    set o {}
    foreach i $a {
       if {[lsearch $b $i] >= 0} {lappend o $i}
    }
    return $o
 }
 proc set:difference {a b} {
    foreach i $b {
       if {[set idx [lsearch $a $i]] >= 0} {
          set a [lreplace $a $idx $idx]
       }
    }
    return $a
 }