The usual difficulty when working with and/or/xor is that most people (or rather most languages including English) doesn't define those words exactly as logical comparison defines them. Usually when people say "OR" what they mean is XOR (or Exclusive Or) - i.e. it's either the one or the other - not both. Logic Comp defines OR as "Any of the operands - even just one, but also more than one".
Then you get to the AND ... which also sometimes gets confusing. Since people sometimes misuse the word in conversation - you end up with situations like these. In Logic Comp AND means "All the operands - not just some".
The way lisp (and most other programming languages) work on these is the following:
AND:
- Check if next operand returns True (or not nil in the case of Lisp).
- If the test is positive, continue. Else return False/Nil.
- If there's any more operands repeat from 1, else return True.
OR:
- Check if next operand returns True (or not nil in the case of Lisp).
- If the test is positive, return True. Else continue.
- If there's any more operands, repeat from 1. Else return False/Nil.
NOT: Basically an invert toggle. If the operand (only one - doesn't work on more) is False/Nil return True. If the operand is True/NotNil return False/Nil.
XOR: This is how most people understand the word OR when spoken - i.e. as in "Either OR".
Though Lisp doesn't have a XOR function, you can make one quite easily. The principle behind it would be to combine OR, NOT & AND. E.g.:- (defun XOR (v1 v2 /) (and (or v1 v2) (not (and v1 v2))))
Thus it returns an AND of an OR & (NOT) AND. I.e. True only if OR returns true, but AND returns FALSE - otherwise return False/Nil.
Most other languages have built-in XOR functions/operators, but usually they'd be implemented similarly - just already done for you. |