Sum and product of an array: Difference between revisions

Content added Content deleted
No edit summary
Line 147: Line 147:
41
41
30030</lang>
30030</lang>

Perhaps better Babel:

<lang babel>main:
{ [2 3 5 7 11 13]
ar2ls dup cp
<- sum_stack ->
prod_stack
%d cr <<
%d cr << }

sum_stack:
{ { give
{ + }
{ depth 1 > }
do_while } nest }

prod_stack:
{ { give
{ * }
{ depth 1 > }
do_while } nest }</lang>

The nest operator creates a kind of argument-passing context -
it saves whatever is on Top-of-Stack (TOS), saves the old stack,
clears the stack and places the saved TOS on the new, cleared stack.
This permits a section to monopolize the stack. At the end of the nest
context, whatever is on TOS will be "passed back" to the original stack
which will be restored.

The depth operator returns the current depth of the stack.


=={{header|BASIC}}==
=={{header|BASIC}}==