function compose(f, g) {
return function(x) { return f(g(x)); };
}
var squared_factorial =
compose(square, factorial);
function sum(term, a, next, b) {
return (a > b) ? 0
: term(a)
+
sum(term, next(a), next, b);
}
function fold(op, f, n) {
return (n === 0)
? f(0)
: op(f(n), fold(op, f, n - 1));
}
function fold(op, f, n) {
return (n === 0)
? f(0)
: op(f(n), fold(op, f, n - 1));
}
function power(b, e) {
return fold(function(x, y) { return x * y; },
function(x) { return b; },
e - 1);
}
function sum_of_digits(n) {
return fold( function(x, y) { return x + y; },
function(k) { return kth_digit(n, k); },
count_digits(n)
);
}
function fold(op, term, a, next, b, base) {
return (a > b)
? base
: op(term(a),
fold(op, term, next(a),
next, b, base));
}
function sum(term, a, next, b) {
return fold( function(x, y) { return x + y; },
term, a, next, b, 0 );
}
/
#