Posted to tcl by kbk at Sun Jul 13 02:56:20 GMT 2008view raw

  1. namespace path ::tcl::mathop
  2. proc lambda {arglist body} {list ::apply [list $arglist $body]}
  3. interp alias {} I {} return -level 0
  4. proc U {f args} {{*}$f $f {*}$args}
  5.  
  6. # recursive factorial
  7.  
  8. puts [U [lambda {f x} {
  9. if {$x == 0} {
  10. I 1
  11. } else {
  12. * $x [U $f [- $x 1]]
  13. }
  14. }] 6]
  15.  
  16. # iterative factorial
  17.  
  18. puts [U [lambda {f y x} {
  19. if {$x == 0} {
  20. I $y
  21. } else {
  22. # next line should be a tailcall
  23. U $f [* $y $x] [- $x 1]
  24. }
  25. }] 1 6]