計算機科学のブログ

Building Abstractions with Data - Hierarchical Data and the Closure Property - Sequences as Conventional Interfaces - Nested Mappings - flatmap, filter, ordered triples, sum

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.3(Sequences as Conventional Interfaces)、Nested Mappings、Exercise 2.41の解答を求めてみる。

コード

function stringfy(x) {
    if (x === undefined) {
        return 'undefined';
    }
    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 display(x) {
    return console.log(stringfy(x));
}
function accumulate(op, initial, sequence) {
    return is_null(sequence) ?
        initial :
        op(
            head(sequence),
            accumulate(op, initial, tail(sequence))
        );
}
function map(f, seq) {
    return accumulate((x, y) => pair(f(x), y), null, seq);
}
function filter(pred, seq) {
    if (is_null(seq)) {
        return null;
    }
    const x = head(seq);
    return pred(x) ?
        pair(x, filter(pred, tail(seq))) :
        filter(pred, tail(seq));
}
function append(seq1, seq2) {
    return accumulate(pair, seq2, seq1)
}
function enumerate_interval(low, high) {
    return low > high ?
        null :
        pair(low, enumerate_interval(low + 1, high));
}
function flatmap(f, seq) {
    return accumulate(append, null, map(f, seq));
}
function is_less_than_n(triple, n) {
    return head(triple) + head(tail(triple)) + head(tail(tail(triple))) <= n;
}
function ordered_triples(n) {
    return filter(
        triple => is_less_than_n(triple, n),
        flatmap(
            a => flatmap(
                b => map(
                    c => list(a, b, c),
                    enumerate_interval(b, n - 2)
                ),
                enumerate_interval(a, n - 2)
            ),
            enumerate_interval(1, n - 2)
        )
    );
}
display(ordered_triples(1));
display(ordered_triples(2));
display(ordered_triples(3));
display(ordered_triples(4));
display(ordered_triples(5));
display(ordered_triples(10));

入出力結果(Terminal, Zsh)

% node answer2.41.js
null
null
[[1, [1, [1, null]]], null]
[[1, [1, [1, null]]], [[1, [1, [2, null]]], null]]
[[1, [1, [1, null]]], [[1, [1, [2, null]]], [[1, [1, [3, null]]], [[1, [2, [2, null]]], null]]]]
[[1, [1, [1, null]]], [[1, [1, [2, null]]], [[1, [1, [3, null]]], [[1, [1, [4, null]]], [[1, [1, [5, null]]], [[1, [1, [6, null]]], [[1, [1, [7, null]]], [[1, [1, [8, null]]], [[1, [2, [2, null]]], [[1, [2, [3, null]]], [[1, [2, [4, null]]], [[1, [2, [5, null]]], [[1, [2, [6, null]]], [[1, [2, [7, null]]], [[1, [3, [3, null]]], [[1, [3, [4, null]]], [[1, [3, [5, null]]], [[1, [3, [6, null]]], [[1, [4, [4, null]]], [[1, [4, [5, null]]], [[2, [2, [2, null]]], [[2, [2, [3, null]]], [[2, [2, [4, null]]], [[2, [2, [5, null]]], [[2, [2, [6, null]]], [[2, [3, [3, null]]], [[2, [3, [4, null]]], [[2, [3, [5, null]]], [[2, [4, [4, null]]], [[3, [3, [3, null]]], [[3, [3, [4, null]]], null]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
%