From b2a3b3a8a2b73649d80ca8a5b34b8242d481e7a3 Mon Sep 17 00:00:00 2001 From: Robby Findler Date: Mon, 20 Feb 2012 06:21:35 -0600 Subject: [PATCH] add xor, adjust implies following Carl's comments --- collects/racket/bool.rkt | 13 ++++++---- collects/scribblings/reference/booleans.scrbl | 24 +++++++++++++++---- collects/tests/racket/bool.rkt | 10 +++++--- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/collects/racket/bool.rkt b/collects/racket/bool.rkt index 6158eb4f7d..9044b2b8eb 100644 --- a/collects/racket/bool.rkt +++ b/collects/racket/bool.rkt @@ -3,7 +3,7 @@ (provide true false false? boolean=? symbol=? - implies nand nor) + implies nand nor xor) (require (for-syntax racket/base)) (define true #t) @@ -24,9 +24,7 @@ (define-syntax (implies stx) (syntax-case stx () [(implies x y) - (syntax/loc stx (if x y #t))] - [(implies x y z w ...) - (syntax/loc stx (if x (implies y z w ...) #t))])) + (syntax/loc stx (if x y #t))])) (define-syntax (nor stx) (syntax-case stx () @@ -35,3 +33,10 @@ (define-syntax (nand stx) (syntax-case stx () [(_ expr ...) (syntax/loc stx (not (and expr ...)))])) + +(define (xor a b) + (if a + (if b + #f + a) + b)) diff --git a/collects/scribblings/reference/booleans.scrbl b/collects/scribblings/reference/booleans.scrbl index 42710745eb..d3bc4248c9 100644 --- a/collects/scribblings/reference/booleans.scrbl +++ b/collects/scribblings/reference/booleans.scrbl @@ -268,17 +268,33 @@ Returns @racket[(not v)].} } -@defform[(implies expr expr expr ...)]{ +@defform[(implies expr1 expr2)]{ Checks to be sure that the first - expression implies the second, implies - the third, etc. + expression implies the second. + + Same as @racket[(if expr1 expr2 #f)]. @examples[#:eval bool-eval (implies #f #t) (implies #f #f) - (implies #f #f #t) (implies #t #f) (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)] + } \ No newline at end of file diff --git a/collects/tests/racket/bool.rkt b/collects/tests/racket/bool.rkt index 33c5d6fc74..d365729d22 100644 --- a/collects/tests/racket/bool.rkt +++ b/collects/tests/racket/bool.rkt @@ -20,9 +20,8 @@ (for ([x (in-list '(#f #t))]) (for ([y (in-list '(#f #t))]) - (for ([z (in-list '(#f #t))]) - (check-equal? (implies x y z) - (or (not x) (or (not y) z)))))) + (check-equal? (implies x y) + (or (not x) y)))) (check-equal? (implies #f (car 'x)) #t) @@ -37,3 +36,8 @@ (check-equal? (nor #f #t) #f) (check-equal? (nor #t #t) #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)