1.3O 고차함수로 만드는 sum함수의 선형 재귀적 과정
function sum(term, a, next, b) {
let iter = (a, res) => {
return a > res ? 0 : term(a) + iter(next(a), b);
};
return iter(a, b);
}
function term(x) {
return x;
}
function next(x) {
return x + 1;
}
console.log(sum(term, 1, next, 10));
곱을 계산하는 재귀적 과정
function mup(term, a, next, b) {
let arr = [];
let iter = (a, res) => {
arr.push(term(a));
if (a > res) console.log("arris", arr);
return a > res ? 1 : term(a) * iter(next(a), b);
};
return iter(a, b);
}
function term(x) {
return x;
}
function next(x) {
return x + 1;
}
console.log(mup(term, 1, next, 10));
팩토리얼 함수
;
function Factorial(Num) {
let Facterm = (x) => x;
let Factnext = (x) => x + 1;
return mup(Facterm, 1, Factnext, Num);
}
console.log("fact", Factorial(10));