Sum and product of an array: Difference between revisions
Content added Content deleted
(→{{header|Scala}}: ++ sather) |
m (→{{header|Scala}}: product must be mutable) |
||
Line 856: | Line 856: | ||
// (_ * _) is a shortcut for {(x,y) => x * y}</lang> |
// (_ * _) is a shortcut for {(x,y) => x * y}</lang> |
||
It may also be done in a classic imperative way : |
It may also be done in a classic imperative way : |
||
<lang scala>var sum = 0; |
<lang scala>var sum = 0; var product = 1 |
||
for (val x <- a) sum = sum + x |
for (val x <- a) sum = sum + x |
||
for (val x <- a) product = product * x</lang> |
for (val x <- a) product = product * x</lang> |