Posted to tcl by aspect at Sat May 21 06:56:44 GMT 2011view raw

  1. proc unknown {cmd args} {
  2. upvar 1 $cmd cmdvar
  3. if {[info exists cmdvar]} {
  4. uplevel 1 [list $cmdvar {*}$args]
  5. } else {
  6. apply $cmd {*}$args
  7. }
  8. }
  9.  
  10. # now we can dispense with [proc]!
  11. set lambda {{args body} {list $args $body}}
  12. set proc {{name args body} {uplevel 1 [list set $name [list $args $body]]}}
  13.  
  14. # scheme's (define) for Tcl
  15. set define {{name value} {
  16. if {[llength $name] > 1} {
  17. set args [lassign $name name]
  18. set value [list $args $body]
  19. }
  20. set $name $value
  21. }
  22.  
  23. define {square a} {* a a}
  24.  
  25. # an accumulator
  26. proc accumulator {initial} {
  27. lambda {addend} {
  28. incr initial $addend
  29. }
  30. }
  31.  
  32. # is there room to attach bytecode to a variable that contains a lambda?
  33.  
  34. # we still lack proper closures. [sproc] seems to have an approach for that.
  35.  
  36. # hygienic macros are completely another story