The following function implements general structural equality
for chapters 1 and 2, assuming that
=== can only be applied
to two numbers or two strings, and otherwise report an error.
The equal function below
will only encounter an error when both
arguments are functions, because it is not clear in chapters 1
and 2 what equality on functions would mean.
次の関数は、第1章と第2章の範囲で一般的な構造的等価性を実装しています。=== は2つの数または2つの文字列にのみ適用でき、それ以外ではエラーを報告すると仮定しています。下記の equal 関数がエラーに遭遇するのは、両方の引数が関数である場合のみです。第1章と第2章では関数に対する等価性が何を意味するかが明確でないためです。
function equal(xs, ys) {
return is_pair(xs)
? (is_pair(ys) &&
equal(head(xs), head(ys)) &&
equal(tail(xs), tail(ys)))
: is_null(xs)
? is_null(ys)
: is_number(xs)
? (is_number(ys) && xs === ys)
: is_boolean(xs)
? (is_boolean(ys) && ((xs && ys) || (!xs && !ys)))
: is_string(xs)
? (is_string(ys) && xs === ys)
: is_undefined(xs)
? is_undefined(ys)
: // we know now that xs is a function
(is_function(ys) && xs === ys);
}
In chapter 3, we
extend === such that it
can be applied to any value and give meaningful results (pointer
equality). The
equal function above will
still be valid with this extended meaning of
===, but can be simplified
as follows:
第3章では、=== を任意の値に適用でき意味のある結果(ポインタの等価性)を返すように拡張します。上記の equal 関数はこの拡張された意味の === でも有効ですが、次のように簡略化できます。
function equal(xs, ys) {
return is_pair(xs)
? (is_pair(ys) &&
equal(head(xs), head(ys)) &&
equal(tail(xs), tail(ys)))
: xs === ys;
}