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

namespace path ::tcl::mathop
proc lambda {arglist body} {list ::apply [list $arglist $body]}
interp alias {} I {} return -level 0
proc U {f args} {{*}$f $f {*}$args}

# recursive factorial

puts [U [lambda {f x} {
    if {$x == 0} {
	I 1
    } else {
	* $x [U $f [- $x 1]]
    }
}] 6]

# iterative factorial

puts [U [lambda {f y x} {
    if {$x == 0} {
	I $y
    } else {
	# next line should be a tailcall
	U $f [* $y $x] [- $x 1]
    }
}] 1 6]