Jump to content

Apply a callback to an array: Difference between revisions

m
→‎{{header|Babel}}: Updated to interactive-mode syntax
No edit summary
m (→‎{{header|Babel}}: Updated to interactive-mode syntax)
Line 261:
=={{header|Babel}}==
 
Let us define a squaring operator:
We can alter the list in place:
 
<lang babel>((mainsq { dup * } <</dup>
{ (1 1 2 3 5 8 13 21) dup
{double !} each
{%d ' ' . <<} each})
 
Now, we apply the sq operator over a list and display the result using the lsnum utility:
(double { dup 2 * set }))</lang>
 
<lang babel>( 0 1 1 2 3 5 8 13 21 34 ) { sq ! } over ! lsnum !</lang>
Or, we can leave the original list unmodified:
 
{{Out}}
<lang babel>((main
<pre>( 0 1 1 {4 (1 19 225 364 5169 8441 131156 21)</pre>
{double !} each
collect !
{%d ' ' . <<} each})
 
(double { 2 * })
 
(collect { -1 take }))</lang>
 
{{Out}} Both versions produce the same output:
<pre>2 2 4 6 10 16 26 42 </pre>
 
Because lists and arrays are distinct in Babel,
we can also do a similar procedure over an array,
with the same results as above:
 
<lang babel>((main
{ [val 1 1 2 3 5 8 13 21]
{double !} eachar
collect !
{%d ' ' . <<} each})
 
(double { 2 * })
 
(collect { -1 take }))</lang>
 
=={{header|BBC BASIC}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.