Precedence Misunderstanding

N

Nick Keighley

Algebra is a branch of mathematics which covers operators and relations
between sets.  These sets aren't limited to the real numbers set, and the
operators aren't limited to the basic mathematical operators that take real
values as operands.  In fact, typically the first contact with algebra tends
not to involve real numbers at all, or basic math operators.  Take, for
example, how Venn diagrams tend to be presented in kindergarten.

and so?
Regarding "side effects", the concepts which I've referred to are indeed
covered, and they tend to be covered right in the very first explanation of
what a function is supposed to be.  Once you were taught that a function f
maps elements in a set A to elements in a set B, you covered all the math
that is needed to explain and understand "side effects".  No new math is
needed to describe a function assigning a certain value to a flag, or
returning a different value depending on the value stored in any other flag.

maths can be used to model almost anything. Including exotic
things like functions with side effects; this doesn't mean basic
mathematics covers side effects in any obvious or intuitive sense.
Anyone whose glanced at formal methods (the maths of programming)
would quickly see this.

There are also functional programming languages that go to great
lengths (cf. Haskell monads) to avoid side effects (so far as they
can) precisely because side effect free programs are easier to "reason
about" (analyse mathematically).
 
P

Phil Carmody

BartC said:
Ike Naar said:
Ben Bacarisse wrote:
[Referring to an expression containing only && and || operators]
an operand that is further to the right is never evaluated before one
to its left.

Except when one to its left doesn't need to be evaluated.

For example, in
(a || b) && c
with a = 1, a is evaluated, b is not evaluated, and c is evaluated.

It's not a counterexample to what Ben said.
Even in your example, no operand is evaluated before one to its left.

How about:

a && 0

or:

a || 1

a needn't be evaluated; but it is.

As if...

Phil
--
I'd argue that there is much evidence for the existence of a God.
Pics or it didn't happen.
-- Tom (/. uid 822)
 
P

Phil Carmody

Willem said:
Charles Richmond wrote:
) The crux of what I do *not* understand is this:
)
) Can someone explain how "&&" can have a *higher* precedence than
) "||"... when any series of AND's and OR's are just going to be
) done left to right???

The precedence determines how the results *will be used*.
It does not determine the order in which they are evaluated.

) For example, if you have a long series of logical AND's and OR's like this:
)
) a && b || c || d && x && y || z
)
) How will "&&" having a higher precedence than "||" effect this at all???

This is, step by step, what the program will do:

/* Construct for the expression a && b || c || d && x && y || z */
int abcdxyz(int a, int b, int c, int d, int x, int y, int z)
{
if (a) {
if (b) {
return 1;
}
}
if (c) {
return 1;
}
if (d) {
if (x) {
if (y) {
return 1;
}
}
}
if (z) {
return 1;
}
return 0;
}

Now, as you can see: a, b, c, d, x, y, z are evaluated in that order.

It's possible to evaluate c when you've not evaluated b. There may be
a weak ordering, but I'm not sure a non-mathematical reading would
include that as an interpretation.
Suppose that '&&' had higher precedence, it would become the following:
s/&&/||/

Phil
Pics or it didn't happen.
-- Tom (/. uid 822)
 
W

Willem

Phil Carmody wrote:
)> This is, step by step, what the program will do:
)>
)> /* Construct for the expression a && b || c || d && x && y || z */
)> int abcdxyz(int a, int b, int c, int d, int x, int y, int z)
)> {
)> if (a) {
)> if (b) {
)> return 1;
)> }
)> }
)> if (c) {
)> return 1;
)> }
)> if (d) {
)> if (x) {
)> if (y) {
)> return 1;
)> }
)> }
)> }
)> if (z) {
)> return 1;
)> }
)> return 0;
)> }
)>
)> Now, as you can see: a, b, c, d, x, y, z are evaluated in that order.
)
) It's possible to evaluate c when you've not evaluated b. There may be
) a weak ordering, but I'm not sure a non-mathematical reading would
) include that as an interpretation.

Yes, I wrote that piece of code to demonstrate exactly what will
happen, precisely because english wording tends to be so ambiguous.

You can either try to precisify the wording, or you can write in code.

)> Suppose that '&&' had higher precedence, it would become the following:
)
) s/&&/||/

Good catch. Or s/higher/lower/, of course.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top