# Experimenting with callbacks. (4.00)
# (The variable name avoids infinite recursion).
namespace eval lambda {
variable id 0
}
proc lambda {capturedList args body} {
set _ _lambda[incr ::lambda::id]
proc $_ $args \
[concat [list foreach {name value} $capturedList {
set $name $value }] \; $body]
return $_
}
proc not1 {unaryPredicate} {
lambda [list predicate $unaryPredicate] {item} {
expr ! [$predicate $item]
}
}
proc any {seq predicate} {
foreach item $seq {
if {[$predicate $item]} {
return 1
}
}
return 0
}
proc all {seq predicate} {
expr ! [any $seq [not1 $predicate]]
}
proc to_bool {i} {
if {$i == 0} { return false }
return true;
}
# Main.
proc stringAnyIs {class s} {
to_bool [any [split $s {}] [lambda [list class $class] {c} {
string is $class $c
}]]
}
proc stringAllIs {class s} {
to_bool [all [split $s {}] [lambda [list class $class] {c} {
string is $class $c
}]]
}
while {[gets stdin line] >= 0} {
foreach class {space graph punct} {
puts "\"$line\" any $class: [stringAnyIs $class $line]"
puts "\"$line\" all $class: [stringAllIs $class $line]"
}
}