- even: divisible by 2 with zero remainder
- prime: >1 and divisible only by 1 and itself
{even {prime 2 0} {prime 3 1}}which might be evaluated boringly as
if {$even} { if {$prime} { return 2 } else { return 0 } } else { if {$prime} { return 3 } else { return 1 } }Now the following recursive procedure converts it into an expression in expr language, which looks like this:
$even? $prime? "2": "0": $prime? "3": "1"The conditions (features) have been prefixed with a dollar sign for variable substitution; the result strings have been quoted so they can have other than numeric values. }
proc dtree2expr dtree { if {[llength $dtree]==3} { foreach {a b c} $dtree break return "\$$a? [dtree2expr $b]: [dtree2expr $c]" } else { return \"$dtree\" } }if 0 {Testing the thing goes with this silly framework, where the prime test was done very cheaply, but sufficient for the task at hand. If all went well, it should report its input back if called with 0..3, and make errors on other input... }
proc classifyNumber x { #-- "feature extraction" set even [expr {$x%2==0}] set prime [expr {[lsearch {2 3 5 7 11 13 ..} $x]>=0}] #-- "classification" set dtree {even {prime 2 0} {prime 3 1}} expr [dtree2expr $dtree] }
Arts and crafts of Tcl-Tk programming