計算機科学のブログ

Building Abstractions with Data - Symbolic Data - Example: Huffman Encoding Trees - Sets of weighted elements - decode

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.3(Symbolic Data)、2.3.4(Example: Huffman Encoding Trees)、Sets of weighted elements、Exercise 2.67の解答を求めてみる。

コード

function stringfy(x) {
    if (is_null(x)) {
        return "null";
    }
    if (is_pair(x)) {
        return "[" + stringfy(head(x)) + ", " + stringfy(tail(x)) + "]";
    }
    if (x === undefined) {
        return "undefined";
    }
    return x.toString();
}
function display(x) {
    return console.log(stringfy(x));
}
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 length(x) {
    function iter(x, n) {
        return is_null(x) ?
            n :
            iter(tail(x), n + 1);
    }
    return iter(x, 0);
}
function list(...args) {
    return args.length === 0 ?
        null :
        pair(args[0], list(...args.slice(1)));
}
function is_null(x) {
    return x === null;
}
function append(list1, list2) {
    return is_null(list1)
        ? list2
        : pair(head(list1), append(tail(list1), list2));
}
function make_leaf(symbol, weight) {
    return list("leaf", symbol, weight);
}
function is_leaf(object) {
    return head(object) === "leaf";
}
function symbol_leaf(x) {
    return head(tail(x));
}
function weight_leaf(x) {
    return head(tail(tail(x)));
}
function make_code_tree(left, right) {
    return list("code_tree", left, right,
        append(symbols(left), symbols(right)),
        weight(left) + weight(right));
}
function left_branch(tree) {
    return head(tail(tree));
}
function right_branch(tree) {
    return head(tail(tail(tree)));
}
function symbols(tree) {
    return is_leaf(tree)
        ? list(symbol_leaf(tree))
        : head(tail(tail(tail(tree))));
}
function weight(tree) {
    return is_leaf(tree)
        ? weight_leaf(tree)
        : head(tail(tail(tail(tail(tree)))));
}
function decode(bits, tree) {
    function decode_1(bits, current_branch) {
        if (is_null(bits)) {
            return null;
        }
        const next_branch = choose_branch(
            head(bits),
            current_branch
        );
        return is_leaf(next_branch)
            ? pair(symbol_leaf(next_branch), decode_1(tail(bits), tree))
            : decode_1(tail(bits), next_branch);
    }
    return decode_1(bits, tree);
}
function choose_branch(bit, branch) {
    return bit === 0
        ? left_branch(branch)
        : bit === 1
            ? right_branch(branch)
            : error(bit, "bad bit -- choose_branch");
}
const sample_tree = make_code_tree(
    make_leaf("A", 4),
    make_code_tree(
        make_leaf("B", 2),
        make_code_tree(
            make_leaf("D", 1),
            make_leaf("C", 12)
        )
    )
);
const sample_message = list(0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0);
display(decode(sample_message, sample_tree));

入出力結果(Terminal, Zsh)

% node answer2.67.js
[A, [D, [A, [B, [B, [C, [A, null]]]]]]]
%