Sum and product of an array: Difference between revisions
Added alternative AppleScript implementation
(Added alternative AppleScript implementation) |
|||
Line 236:
set product to product * i
end repeat</lang>
Condensed version of above, which also prints the results :
<lang AppleScript>
set {array, sum, product} to {{1, 2, 3, 4, 5}, 0, 1}
repeat with i in array
set {sum, product} to {sum + i, product * i}
end repeat
return sum & " , " & product as string
</lang>
{{out}}
<pre>
"15 , 120"
</pre>
Or, using an AppleScript implementation of '''fold'''/'''reduce''':
|