Runtime evaluation/In an environment: Difference between revisions

Content added Content deleted
(→‎{{header|Octave}}: changed into an imitation of python, more or less)
(→‎{{header|Tcl}}: (before: bad understanding of the tcl code? note the apply) maybe the error was elsewhere; now hopefully fixed)
Line 179: Line 179:


=={{header|Tcl}}==
=={{header|Tcl}}==

{{incorrect|Tcl|The code provided should not itself be a function, but have x as a free variable.}}


<lang tcl>package require Tcl 8.5
<lang tcl>package require Tcl 8.5


proc eval_twice {func a b} {
proc evalit {func a} {
set 1st [apply $func $a]
set r [apply $func $a]
set 2nd [apply $func $b]
expr {$r}
expr {$2nd - $1st}
}
}


eval_twice {x {expr {2 ** $x}}} 3 5 ;# ==> 24</lang>
set a [evalit {x {expr {2 ** $x}}} 3];
set b [evalit {x {expr {2 ** $x}}} 5];
puts [expr {$b - $a}];
</lang>