# Experimenting with callbacks. (4.01)
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 $_
}
interp alias {} λ {} lambda
proc not1 {unaryPredicate} {
λ [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]]
}
# Main.
proc toBool {i} {
if {$i == 0} { return false }
return true;
}
proc stringAnyIs {class s} {
toBool [any [split $s {}] [λ [list class $class] {c} {
string is $class $c
}]]
}
proc stringAllIs {class s} {
toBool [all [split $s {}] [λ [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]"
}
}