[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
6.23.2.2 Equality
Since Scheme’s equal?
must be transitive, and '()
is not equal?
to #f
, to Scheme nil
is not
equal?
to #f
or '()
.
(eq? #f '()) ⇒ #f (eq? #nil '()) ⇒ #f (eq? #nil #f) ⇒ #f (eqv? #f '()) ⇒ #f (eqv? #nil '()) ⇒ #f (eqv? #nil #f) ⇒ #f (equal? #f '()) ⇒ #f (equal? #nil '()) ⇒ #f (equal? #nil #f) ⇒ #f
However, in Elisp, '()
, #f
, and nil
are all
equal
(though not eq
).
(defvar f (make-scheme-false)) (defvar eol (make-scheme-null)) (eq f eol) ⇒ nil (eq nil eol) ⇒ nil (eq nil f) ⇒ nil (equal f eol) ⇒ t (equal nil eol) ⇒ t (equal nil f) ⇒ t
These choices facilitate interoperability between Elisp and Scheme code, but they are not perfect. Some code that is correct standard Scheme is not correct in the presence of a second false and null value. For example:
(define (truthiness x) (if (eq? x #f) #f #t))
This code seems to be meant to test a value for truth, but now that
there are two false values, #f
and nil
, it is no longer
correct.
Similarly, there is the loop:
(define (my-length l) (let lp ((l l) (len 0)) (if (eq? l '()) len (lp (cdr l) (1+ len)))))
Here, my-length
will raise an error if l is a
nil
-terminated list.
Both of these examples are correct standard Scheme, but, depending on
what they really want to do, they are not correct Guile Scheme.
Correctly written, they would test the properties of falsehood or
nullity, not the individual members of that set. That is to say, they
should use not
or null?
to test for falsehood or nullity,
not eq?
or memv
or the like.
Fortunately, using not
and null?
is in good style, so all
well-written standard Scheme programs are correct, in Guile Scheme.
Here are correct versions of the above examples:
(define (truthiness* x) (if (not x) #f #t)) ;; or: (define (t* x) (not (not x))) ;; or: (define (t** x) x) (define (my-length* l) (let lp ((l l) (len 0)) (if (null? l) len (lp (cdr l) (1+ len)))))
This problem has a mirror-image case in Elisp:
(deffn my-falsep (x) (if (eq x nil) t nil))
Guile can warn when compiling code that has equality comparisons with
#f
, '()
, or nil
. See section Compiling Scheme Code, for details.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on April 20, 2013 using texi2html 5.0.