Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | 1x 8x 4x 4x 22x 1x 14x 4x 4x 1x 7x 4x 1x 5x 4x 1x 2x 2x 8x 5x 3x 3x 3x 2x | const cons = (a, b) => {
const pair = f => f(a, b);
pair.isPair = true;
return pair;
};
const isPair = pair => typeof pair === 'function' && pair.isPair === true;
const checkPair = (pair) => {
if (!isPair(pair)) {
const value = typeof pair === 'object' ? JSON.stringify(pair, null, 2) : String(pair);
throw new Error(`Argument must be pair, but it was '${value}'`);
}
};
const car = (pair) => {
checkPair(pair);
return pair(a => a);
};
const cdr = (pair) => {
checkPair(pair);
return pair((a, b) => b);
};
const toString = (pair) => {
checkPair(pair);
const iter = (p) => {
if (!isPair(p)) {
return String(p);
}
const left = car(p);
const right = cdr(p);
return `(${iter(left)}, ${iter(right)})`;
};
return iter(pair);
};
export {
cons, car, cdr, isPair, toString, checkPair,
};
|