計算機科学のブログ

Building Abstractions with Data - Symbolic Data - Example: Representing Sets - Sets and information retrieval - recrods, key, value, lookup

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.3(Example: Representing Sets)、Sets and information retrieval、Exercise 2.66の解答を求めてみる。

コード

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 make_tree(entry, left, right) {
    return list(entry, left, right);
}
function entry(tree) {
    return head(tree);
}
function left_branch(tree) {
    return head(tail(tree));
}
function right_branch(tree) {
    return head(tail(tail(tree)));
}
function make_tree(entry, left, right) {
    return list(entry, left, right);
}
function list_to_tree(elements) {
    return head(partial_tree(elements, length(elements)));
}
function partial_tree(elts, n) {
    if (n === 0) {
        return pair(null, elts);
    }
    const left_size = Math.floor((n - 1) / 2);
    const left_result = partial_tree(elts, left_size);
    const left_tree = head(left_result);
    const non_left_elts = tail(left_result);
    const right_size = n - (left_size + 1);
    const this_entry = head(non_left_elts);
    const right_result = partial_tree(tail(non_left_elts), right_size);
    const right_tree = head(right_result);
    const remaining_elts = tail(right_result);
    return pair(
        make_tree(this_entry, left_tree, right_tree),
        remaining_elts
    );
}
function make_record(key, value) {
    return list(key, value);
}
function key(record) {
    return head(record);
}
function value(record) {
    return head(tail(record));
}
function lookup(given_key, set_of_records) {
    if (is_null(set_of_records)) {
        return false;
    }
    const e = entry(set_of_records);
    const k = key(e);
    return given_key < k
        ? lookup(given_key, left_branch(set_of_records))
        : given_key === k
            ? e
            : lookup(given_key, right_branch(set_of_records));
}
const records = list(
    make_record(1, "a"),
    make_record(2, "b"),
    make_record(3, "c"),
    make_record(4, "d"),
    make_record(5, "e"),
    make_record(6, "f"),
    make_record(7, "g"),
    make_record(8, "a"),
    make_record(9, "b"),
    make_record(10, "c")
);
const tree = list_to_tree(records);

display(records);
display(lookup(0, tree));
display(lookup(1, tree));
display(lookup(2, tree));
display(lookup(5, tree));
display(lookup(6, tree));
display(lookup(7, tree));
display(lookup(11, tree));
display(lookup(20, tree));

入出力結果(Terminal, Zsh)

% node answer2.66.js
[[1, [a, null]], [[2, [b, null]], [[3, [c, null]], [[4, [d, null]], [[5, [e, null]], [[6, [f, null]], [[7, [g, null]], [[8, [a, null]], [[9, [b, null]], [[10, [c, null]], null]]]]]]]]]]
false
[1, [a, null]]
[2, [b, null]]
[5, [e, null]]
[6, [f, null]]
[7, [g, null]]
false
false
%