function sum_integers(a, b) {
return a > b
? 0
: a + sum_integers(a + 1, b);
}
function sum_cubes(a, b) {
return a > b
? 0
: cube(a) + sum_cubes(a + 1, b);
}
function pi_sum(a, b) {
return a > b
? 0
: 1 / (a * (a + 2)) + pi_sum(a + 4, b);
}
sigma notation,for example \[\begin{array}{lll} \displaystyle\sum_{n=a}^{b}\ f(n)&=&f(a)+\cdots+f(b) \end{array}\] to express this concept. The power of sigma notation is that it allows mathematicians to deal with the concept of summation itself rather than only with particular sums—for example, to formulate general results about sums that are independent of the particular series being summed.
slotsinto parameters:
function sum(term, a, next, b) {
return a > b
? 0
: term(a) + sum(term, next(a), next, b);
}function inc(n) {
return n + 1;
}
function sum_cubes(a, b) {
return sum(cube, a, inc, b);
}
sum_cubes(1, 10);
3025
function identity(x) {
return x;
}
function sum_integers(a, b) {
return sum(identity, a, inc, b);
}
sum_integers(1, 10);
55
function pi_sum(a, b) {
function pi_term(x) {
return 1 / (x * (x + 2));
}
function pi_next(x) {
return x + 4;
}
return sum(pi_term, a, pi_next, b);
}
8 * pi_sum(1, 1000);
3.139592655589783
function integral(f, a, b, dx) {
function add_dx(x) {
return x + dx;
}
return sum(f, a + dx / 2, add_dx, b) * dx;
}
integral(cube, 0, 1, 0.01);
0.24998750000000042
integral(cube, 0, 1, 0.001);
0.249999875000001
function inc(k) {
return k + 1;
}
function simpsons_rule_integral(f, a, b, n) {
function helper(h) {
function y(k) {
return f((k * h) + a);
}
function term(k) {
return k === 0 || k === n
? y(k)
: k % 2 === 0
? 2 * y(k)
: 4 * y(k);
}
return sum(term, 0, inc, n) * (h / 3);
}
return helper((b - a) / n);
}
function sum(term, a, next, b) {
function iter(a, result) {
return a > b
? result
: iter(next(a), result + term(a));
}
return iter(a, 0);
}
accumulate(combiner, null_value, term, a, next, b);
function filtered_accumulate(combiner, null_value,
term, a, next, b, filter) {
return a > b
? null_value
: filter(a)
? combiner(term(a),
filtered_accumulate(combiner, null_value,
term, next(a), next,
b, filter))
: filtered_accumulate(combiner, null_value,
term, next(a), next,
b, filter);
}
//recursive process
function accumulate_r(combiner, null_value, term, a, next, b) {
return a > b
? null_value
: combiner(term(a),
accumulate_r(combiner,
null_value,
term, next(a), next, b));
}
function sum_r(term, a, next, b) {
function plus(x, y) {
return x + y;
}
return accumulate_r(plus, 0, term, a, next, b);
}
function product_r(term, a, next, b) {
function times(x, y) {
return x * y;
}
return accumulate_r(times, 1, term, a, next, b);
}
//iterative process
function accumulate_i(combiner, null_value, term, a, next, b) {
function iter(a, result) {
return a > b
? result
: iter(next(a), combiner(term(a), result));
}
return iter(a, null_value);
}
function sum_i(term, a, next, b) {
function plus(x, y) {
return x + y;
}
return accumulate_i(plus, 0, term, a, next, b);
}
function product_i(term, a, next, b) {
function times(x, y) {
return x * y;
}
return accumulate_i(times, 1, term, a, next, b);
}
//recursive process
function product_r(term, a, next, b) {
return a > b
? 1
: term(a) * product_r(term, next(a), next, b);
}
//iterative process
function product_i(term, a, next, b) {
function iter(a, result) {
return a > b
? result
: iter(next(a), term(a) * result);
}
return iter(a, 1);
}