Anonymous user
Closures/Value capture: Difference between revisions
no edit summary
(Added zkl) |
No edit summary |
||
Line 18:
<lang Axiom>[1,4,9,16,25,36,49,64,81,100]
Type: List(Integer)</lang>
=={{header|Babel}}==
<lang babel>((main {
{ iter
1 take bons 1 take
dup cp
{*} cp
3 take
append }
10 times
collect !
{eval %d nl <<} each }))</lang>
This program produces the following output:
<lang babel>
((main {
{ iter
1 take bons 1 take
dup cp
{*} cp
3 take
append }
10 times
collect !
{eval %d nl <<} each })
</lang>
This program produces the following output:
<lang babel>100
81
64
49
36
25
16
9
4
1</lang>
Essentially, a function has been constructed for each value to be squared (10 down to 1). The cp operator ensures that we generate a fresh copy of the number to be squared, as well as the code for multiplying, {*}. In the final each loop, we eval each of the constructed functions and output the result.
=={{header|C}}==
|