Sum and product of an array: Difference between revisions

Added FALSE
(Added FALSE)
Line 523:
<lang factor>: sum ( seq -- n ) 0 [ + ] reduce ;
: product ( seq -- n ) 1 [ * ] reduce ;</lang>
 
=={{header|FALSE}}==
Strictly speaking, there are no arrays in FALSE. However, a number of elements on the stack could be considered an array. The implementation below assumes the length of the array on top of the stack, and the actual items below it. Note that this implementation does remove the "array" from the stack, so in case the original values need to be retained, a copy should be provided before executing this logic.
<lang false>1 2 3 4 5 {input "array"}
5 {length of input}
0s: {sum}
1p: {product}
 
[$0=~][1-\$s;+s:p;*p:]#%
 
"Sum: "s;."
Product: "p;.</lang>
{{out}}
<pre>Sum: 15
Product: 120</pre>
 
=={{header|Fantom}}==
Anonymous user