計算機科学のブログ

Building Abstractions with Data - Hierarchical Data and the Closure Property - Hierarchical Structures - binary mobile, branches, length, structure, weight, balance

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.2(Hierarchical Structures)、Exercise 2.29の解答を求めてみる。

コード

function stringfy(x) {
    if (is_null(x)) {
        return "null";
    }
    if (is_pair(x)) {
        return "[" + stringfy(head(x)) + ", " + stringfy(tail(x)) + "]";
    }
    return x.toString();
}
function pair(x, y) {
    return [x, y];
}
function head(z) {
    return z[0];
}
function tail(z) {
    return z[1];
}
function is_pair(x) {
    return Array.isArray(x);
}
function is_null(x) {
    return x === null;
}
function list(...args) {
    return args.length === 0 ?
        null :
        pair(args[0], list(...args.slice(1)));
}
function list_ref(items, n) {
    return n === 0 ?
        head(items) :
        list_ref(tail(items), n - 1);
}
function is_list(x) {
    return is_null(x) ||
        (is_pair(x) && is_list(tail(x)));
}
function display(x) {
    return console.log(stringfy(x));
}
function append(list1, list2) {
    return is_null(list1) ?
        list2 :
        pair(head(list1), append(tail(list1), list2));
}
function make_mobile(left, right) {
    return list(left, right);
}
// a.
function left_branch(mobile) {
    return list_ref(mobile, 0);
}
function right_branch(mobile) {
    return list_ref(mobile, 1);
}
function make_branch(length, structure) {
    return list(length, structure);
}
function branch_length(branch) {
    return list_ref(branch, 0);
}
function branch_structure(branch) {
    return list_ref(branch, 1);
}
function is_mobile(structure) {
    return is_pair(structure);
}
// b.
function branch_weight(branch) {
    const structure = branch_structure(branch);
    return is_mobile(structure) ?
        total_weight(structure) :
        structure;
}
function total_weight(mobile) {
    return branch_weight(left_branch(mobile)) +
        branch_weight(right_branch(mobile));
}

const mobile = make_mobile(
    make_branch(1, 2),
    make_branch(
        3,
        make_mobile(
            make_branch(4, 5),
            make_branch(6, 7)
        )
    )
);

display(total_weight(mobile));

// c.
function torque(branch) {
    return branch_length(branch) * branch_weight(branch);
}
function is_balanced_structure(structure) {
    return !is_mobile(structure) || is_balanced(structure);
}
function is_balanced(mobile) {
    const lb = left_branch(mobile);
    const rb = right_branch(mobile);
    return torque(lb) === torque(rb) &&
        is_balanced_structure(branch_structure(lb)) &&
        is_balanced_structure(branch_structure(rb));
}
display(is_balanced(mobile));
const mobile1 = make_mobile(
    make_branch(1, 36),
    make_branch(
        3,
        make_mobile(
            make_branch(7, 5),
            make_branch(5, 7)
        )
    )
);
display(is_balanced(mobile1));

// d.
function make_mobile1(left, right) {
    return pair(left, right);
}
// changed
function left_branch1(mobile) {
    return head(left);
}
// changed
function right_branch1(mobile) {
    return tail(mobile);
}
function make_branch1(length, structure) {
    return pair(length, structure);
}
// changed
function branch_length1(branch) {
    return head(branch);
}
// changed
function branch_structure1(branch) {
    return tail(branch);
}

入出力結果(Terminal, Zsh)

% node answer2.29.js
14
false
true
%