8. 総和・総乗
func sum(n: Int) -> Int {
var accumulator = 0
for i in 1...n { accumulator += i }
return accumulator
}
func prod(n: Int) -> Int {
var accumulator = 1
for i in 1...n { accumulator *= i }
return accumulator
}
ループと代入を使った手続き的なプログラム
8
35. 参考文献
• Structure and Interpreta.on of Computer Programs
(SICP) 真鍋宏史日本語訳版
• The Swi< Programming Language (Swi< 2.1)
• Swi< Standard Library Reference
35