計算機科学のブログ

Building Abstractions with Data - Hierarchical Data and the Closure Property - Example: A Picture Language - The picture language - beside, below

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)、The picture language、Exercise 2.44の解答を求めてみる。

コード

const painter = 'painter';
function beside(painter1, painter2) {
    return `beside(${painter1}, ${painter2})\n`
}
function below(painter1, painter2) {
    return `below(${painter1}, ${painter2})\n`
}
function up_split(painter, n) {
    if (n === 0) {
        return painter;
    }
    const smaller = up_split(painter, n - 1);
    return below(painter, beside(smaller, smaller));
}
console.log(up_split(painter, 2));

入出力結果(Terminal, Zsh)

% node answer2.44.js
below(painter, beside(below(painter, beside(painter, painter)
)
, below(painter, beside(painter, painter)
)
)
)

%