Version 1 of forall

Updated 2002-05-15 07:57:18

Richard Suchenwirth 2002-05-15 - In return, Lars Hellstr�m used special return codes to handle questions like "for all a in A there exists some b in B such that for all c in C the condition P(a,b,c) holds". It appears as if the same task can be done with nested proc calls (and regular returns), while looking even more similar to the original task specification:

 forall a $A {exists b $B {forall c $C {P $a $b $c}}}

 proc forall {elementName set condition} {
    upvar 1 $elementName element
    foreach element $set {
        if {![uplevel 1 $condition]} {return 0}
    }
    return 1
 }
 proc exists {elementName set condition} {
    upvar 1 $elementName element
    foreach element $set {
        if {[uplevel 1 $condition]} {return 1}
    }
    return 0

 }
 #--------------- self-test
 if {[file tail [info script]]==[file tail $argv0]} {
    proc P {x y z} {
        expr {$x<3 && $y==5 && $z>6}
    }
    set A {1 2 3}
    set B {4 5 6}
    set C {7 8 9}
    set res [forall a $A {
        exists b $B {
            forall c $C {P $a $b $c}
        }
    }]
    puts res:$res 
 }

Arts and crafts of Tcl-Tk programming