Building Abstractions with Data - Hierarchical Data and the Closure Property - Sequences as Conventional Interfaces - Nested Mappings - flatmap, unique pairs
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.40の解答を求めてみる。
コード
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 unique_pairs(n) {
return flatmap(
i => map(
j => list(i, j),
enumerate_interval(1, i - 1)
),
enumerate_interval(1, n)
);
}
function make_pair_sum(pair) {
const x = head(pair);
const y = head(tail(pair));
return list(x, y, x + y);
}
function smallest_divisor(n) {
return find_divisor(n, 2);
}
function divides(a, b) {
return b % a === 0;
}
function square(x) {
return x * x;
}
function find_divisor(n, test_divisor) {
return square(test_divisor) > n ?
n :
divides(test_divisor, n) ?
test_divisor :
find_divisor(n, test_divisor + 1);
}
function is_prime(n) {
return n === smallest_divisor(n);
}
function is_prime_sum(pair) {
return is_prime(head(pair) + head(tail(pair)));
}
function make_pair_sum(pair) {
const x = head(pair);
const y = head(tail(pair));
return list(x, y, x + y);
}
function prime_sum_pairs(n) {
return map(
make_pair_sum,
filter(
is_prime_sum,
unique_pairs(n)
)
);
}
display(unique_pairs(0));
display(unique_pairs(1));
display(unique_pairs(2));
display(unique_pairs(6));
display(prime_sum_pairs(6));
入出力結果(Terminal, Zsh)
% node answer2.40.js
null
null
[[2, [1, null]], null]
[[2, [1, null]], [[3, [1, null]], [[3, [2, null]], [[4, [1, null]], [[4, [2, null]], [[4, [3, null]], [[5, [1, null]], [[5, [2, null]], [[5, [3, null]], [[5, [4, null]], [[6, [1, null]], [[6, [2, null]], [[6, [3, null]], [[6, [4, null]], [[6, [5, null]], null]]]]]]]]]]]]]]]
[[2, [1, [3, null]]], [[3, [2, [5, null]]], [[4, [1, [5, null]]], [[4, [3, [7, null]]], [[5, [2, [7, null]]], [[6, [1, [7, null]]], [[6, [5, [11, null]]], null]]]]]]]
%