add xor, adjust implies following Carl's comments

This commit is contained in:
Robby Findler 2012-02-20 06:21:35 -06:00
parent 297db9b305
commit b2a3b3a8a2
3 changed files with 36 additions and 11 deletions

View File

@ -3,7 +3,7 @@
(provide true false false? (provide true false false?
boolean=? boolean=?
symbol=? symbol=?
implies nand nor) implies nand nor xor)
(require (for-syntax racket/base)) (require (for-syntax racket/base))
(define true #t) (define true #t)
@ -24,9 +24,7 @@
(define-syntax (implies stx) (define-syntax (implies stx)
(syntax-case stx () (syntax-case stx ()
[(implies x y) [(implies x y)
(syntax/loc stx (if x y #t))] (syntax/loc stx (if x y #t))]))
[(implies x y z w ...)
(syntax/loc stx (if x (implies y z w ...) #t))]))
(define-syntax (nor stx) (define-syntax (nor stx)
(syntax-case stx () (syntax-case stx ()
@ -35,3 +33,10 @@
(define-syntax (nand stx) (define-syntax (nand stx)
(syntax-case stx () (syntax-case stx ()
[(_ expr ...) (syntax/loc stx (not (and expr ...)))])) [(_ expr ...) (syntax/loc stx (not (and expr ...)))]))
(define (xor a b)
(if a
(if b
#f
a)
b))

View File

@ -268,17 +268,33 @@ Returns @racket[(not v)].}
} }
@defform[(implies expr expr expr ...)]{ @defform[(implies expr1 expr2)]{
Checks to be sure that the first Checks to be sure that the first
expression implies the second, implies expression implies the second.
the third, etc.
Same as @racket[(if expr1 expr2 #f)].
@examples[#:eval @examples[#:eval
bool-eval bool-eval
(implies #f #t) (implies #f #t)
(implies #f #f) (implies #f #f)
(implies #f #f #t)
(implies #t #f) (implies #t #f)
(implies #f (error 'ack "we don't get here"))] (implies #f (error 'ack "we don't get here"))]
}
@defproc[(xor [b1 any/c] [b2 any/c]) any]{
Returns the exclusive or of @racket[b1] and @racket[b2].
If exactly one of @racket[b1] and @racket[b2] is
not @racket[#f], then return it. Otherwise, returns
@racket[#f].
@examples[#:eval
bool-eval
(xor 11 #f)
(xor #f 22)
(xor 11 22)
(xor #f #f)]
} }

View File

@ -20,9 +20,8 @@
(for ([x (in-list '(#f #t))]) (for ([x (in-list '(#f #t))])
(for ([y (in-list '(#f #t))]) (for ([y (in-list '(#f #t))])
(for ([z (in-list '(#f #t))]) (check-equal? (implies x y)
(check-equal? (implies x y z) (or (not x) y))))
(or (not x) (or (not y) z))))))
(check-equal? (implies #f (car 'x)) #t) (check-equal? (implies #f (car 'x)) #t)
@ -37,3 +36,8 @@
(check-equal? (nor #f #t) #f) (check-equal? (nor #f #t) #f)
(check-equal? (nor #t #t) #f) (check-equal? (nor #t #t) #f)
(check-equal? (nor #t (car 'x)) #f) (check-equal? (nor #t (car 'x)) #f)
(check-equal? (xor 11 22) #f)
(check-equal? (xor 11 #f) 11)
(check-equal? (xor #f 22) 22)
(check-equal? (xor #f #f) #f)