John Lao
John Lao

Reputation: 3

Can anyone help me with this simple boolean scheme code?

I need to define XOR3 that takes boolean inputs a b and c that will return true when exactly one input is true and false otherwise. Been stuck on this forever!

So far, I have (define XOR3 (lambda (a b c) but idk what to put after because I cannot figure out how to get true when only one value is true. I am only supposed to use "or" "and" and "not". Any help appreciated!

Upvotes: 0

Views: 2331

Answers (5)

rnso
rnso

Reputation: 24565

One can also put these items in a list and use count function:

(define (f a b c)
  (if (= 1
         (count
          (lambda(x) x)
          (list a b c)))
      #t #f ))

Or, simplified version suggested by @AlexKnauth in comments below:

(define (f a b c)
  (= 1
     (count
       (lambda(x) x)
       (list a b c) )))

Testing:

(f #t #f #f)
(f #t #t #f)

Output:

#t
#f

This function is highly flexible and can be used for testing any number of items for any number of true values.

Also see: Functional variant of 'oneof' function in Racket

Upvotes: 0

Sylwester
Sylwester

Reputation: 48745

The straightforward solution would be to use or with and and not since your procedure should be true when a is true and the rest is false OR b is true and the rest is false OR c is true and the rest is false.

(define (xor3 a b c) 
  (or (and a (not b) (not c)) 
      (and b (not a) (not c)) 
      (and c (not a) (not b)))) 

Upvotes: 0

Kiran Yadav
Kiran Yadav

Reputation: 1

bool a=true, b=false, c=false;

Label1.Text = ((a == true && b == false && c == false) 
            || (a == false && b == true && c == false) 
            || (a == false && b == false && c == true) ? true : false).ToString();

Upvotes: -3

Mulan
Mulan

Reputation: 135287

Just start with the simplest case and keep narrowing it down

(define (xor3 a b c)
  (cond [(and a b) #f]           ; explanation step 1
        [(false? c) (or a b)]    ; explanation step 2
        [else (not (or a b))]))  ; explanation step 3

(xor3 #t #t #t) ; => #f
(xor3 #t #t #f) ; => #f
(xor3 #t #f #t) ; => #f
(xor3 #t #f #f) ; => #t
(xor3 #f #t #t) ; => #f
(xor3 #f #t #f) ; => #t
(xor3 #f #f #t) ; => #t
(xor3 #f #f #f) ; => #f

Here's the explanation of how my brain processed this:

  1. if a and b are both#t, it doesn't matter what c is, return #f
  2. now we only know that a and b aren't both true, so if c is #f we can just return (or a b)
  3. otherwise c is #t which means neither a or b can be true – instead of (not (or a b)) you can think of this as (and (not a) (not b)) if you'd like.

Remember to always work with your truth table like I did above so that you can verify the validity of your procedure for all possible assignments of a, b, and c


Of course your next logical step is to create a generic procedure which accepts any number of arguments; not just 3

(define (xor a b . xs)
  (cond [(and a b)   #f]
        [(empty? xs) (or a b)]
        [else        (apply xor (or a b) (car xs) (cdr xs))]))

Don't forget to check the truth table

(xor #t #t #t #t) ; => #f
(xor #t #t #t #f) ; => #f
(xor #t #t #f #t) ; => #f
(xor #t #t #f #f) ; => #f
(xor #t #f #t #t) ; => #f
(xor #t #f #t #f) ; => #f
(xor #t #f #f #t) ; => #f
(xor #t #f #f #f) ; => #t
(xor #f #t #t #t) ; => #f
(xor #f #t #t #f) ; => #f
(xor #f #t #f #t) ; => #f
(xor #f #t #f #f) ; => #t
(xor #f #f #t #t) ; => #f
(xor #f #f #t #f) ; => #t
(xor #f #f #f #t) ; => #t
(xor #f #f #f #f) ; => #f

Upvotes: 2

molbdnilo
molbdnilo

Reputation: 66431

Break it down into smaller problems.

What does it mean that exacly one of a, b, c is true?
It means that only a is true, OR only b is true, OR only c is true.
So your function will have the structure

(define (xor3 a b c)
    (or only-a
        only-b
        only-c))

Next step: what does it mean that only a is true?
It means that a is true, AND b isn't true, AND c isn't true.
That is,

(and a (not b) (not c))

so you can plug that into the function.

Similarly for b and c.

Upvotes: 3

Related Questions