計算機科学のブログ

Building Abstractions with Data - Hierarchical Data and the Closure Property - Example: A Picture Language - Higher-order operations - split

Structure and Interpretation of Computer Programs: JavaScript Edition(Harold Abelson(著)、Gerald Jay Sussman(著)、Julie Sussman(著)、The MIT Press)のChapter 2(Building Abstractions with Data)、2.2(Hierarchical Data and the Closure Property)、2.2.4(Example: A Picture Language)、Higher-order operations、Exercise 2.45の解答を求めてみる。

コード

function split(f, g) {
    return (painter, n) => {
        if (n === 0) {
            return painter;
        }
        const smaller = split(f, g)(painter, n - 1);
        return f(painter, g(smaller, smaller));
    };
}