Kaz Kylheku
4/4/2016 3:30:00 PM
On 2016-04-04, Jim Newton <jimka.issy@gmail.com> wrote:
> Interesting that ruby provides this. Most infix syntax languages I've used, does not allow it.
> Many languages have assignment operators such as +=, *=, /=, |= and &= but omit ||= and &&=.
That's simply because the syntax comes from C, which omits it. C omits
it because && and || are not arithmetic operators, but sequencing
operators: A && B ensures that B is not evaluated if A yields false.
Moreover, A && B does not have the type of A or B. It yields an int
(bool as of C99 and in C++) result no matter what are A or B.
So A &&= B doesn't generalize well; it makes sense only when A has a
type which is compatible A && B.
In 27 years of C programming, I have written this, I think,
once or twice, and may have seen this once in someone else's
code:
int A = some_condition();
/*...*/
A = A && other_condition();
/*...*/
A = A && third_condition();
The code elided by /*...*/ somehow makes it necessary. There are
some other conditions which cause some of these assignments to
be skipped or whatever:
int A = 1;
if (foo)
A = A && some_condition();
if (bar)
A = A && other_condition();
if (xyzzy)
A = A && third_condition();
If, say, foo and bar are false, then third_condition() still potentially
evaluates, and A can be true.
In CL we could just do this:
(and (if foo (some-condition) t)
(if bar (other-condition) t)
(if xyzzy (third-condition t)))
What would slightly help here would be a logical form of if which
follows the standard truth table:
A B (lif A B)
-------------------
nil nil t
nil t t
t nil nil
t t t
We have the equivalence (lif A B) <--> (or (not a) b), but that's
slightly verbose:
(and (or (not foo) (some-condition))
(or (not bar) (other-condition))
(or (not xyzzy) (third-condition)))
This translates back to C nicely:
(!foo || some_condition()) &&
(!bar || other_condition()) &&
(!xyzzy || third_condition())
If you think you need &&= in C, you perhaps didn't think hard enough, or
you are slavishly following some coding convention which proscribes long
expressions that break across several lines.