fork(2) download
  1. # Experimenting with callbacks. (4.00)
  2. # (The variable name avoids infinite recursion).
  3.  
  4. namespace eval lambda {
  5. variable id 0
  6. }
  7.  
  8. proc lambda {capturedList args body} {
  9. set _ _lambda[incr ::lambda::id]
  10. proc $_ $args \
  11. [concat [list foreach {name value} $capturedList {
  12. set $name $value }] \; $body]
  13. return $_
  14. }
  15.  
  16. proc not1 {unaryPredicate} {
  17. lambda [list predicate $unaryPredicate] {item} {
  18. expr ! [$predicate $item]
  19. }
  20. }
  21.  
  22. proc any {seq predicate} {
  23. foreach item $seq {
  24. if {[$predicate $item]} {
  25. return 1
  26. }
  27. }
  28. return 0
  29. }
  30.  
  31. proc all {seq predicate} {
  32. expr ! [any $seq [not1 $predicate]]
  33. }
  34.  
  35. proc to_bool {i} {
  36. if {$i == 0} { return false }
  37. return true;
  38. }
  39.  
  40. # Main.
  41.  
  42. proc stringAnyIs {class s} {
  43. to_bool [any [split $s {}] [lambda [list class $class] {c} {
  44. string is $class $c
  45. }]]
  46. }
  47.  
  48. proc stringAllIs {class s} {
  49. to_bool [all [split $s {}] [lambda [list class $class] {c} {
  50. string is $class $c
  51. }]]
  52. }
  53.  
  54. while {[gets stdin line] >= 0} {
  55. foreach class {space graph punct} {
  56. puts "\"$line\" any $class: [stringAnyIs $class $line]"
  57. puts "\"$line\" all $class: [stringAllIs $class $line]"
  58. }
  59. }
Success #stdin #stdout 0.01s 5284KB
stdin
     
abcde
!@#%&
ab de
!@ %&
stdout
"     " any space: true
"     " all space: true
"     " any graph: false
"     " all graph: false
"     " any punct: false
"     " all punct: false
"abcde" any space: false
"abcde" all space: false
"abcde" any graph: true
"abcde" all graph: true
"abcde" any punct: false
"abcde" all punct: false
"!@#%&" any space: false
"!@#%&" all space: false
"!@#%&" any graph: true
"!@#%&" all graph: true
"!@#%&" any punct: true
"!@#%&" all punct: true
"ab de" any space: true
"ab de" all space: false
"ab de" any graph: true
"ab de" all graph: false
"ab de" any punct: false
"ab de" all punct: false
"!@ %&" any space: true
"!@ %&" all space: false
"!@ %&" any graph: true
"!@ %&" all graph: false
"!@ %&" any punct: true
"!@ %&" all punct: false