1. Re: Short circuiting and boolean algebra

At 22:18 26-06-98 -0700, you wrote:
>    My understanding is that you are saying I would like to evaluate "a"
>expression by saying:
>    if a =  1 then
>        do this
>    elsif a= 1 and a = 2 then
>        do that
>            exit
>     elsif a = 1 and a = 2 and a = 3 then
>        do the other thingamajig
>            exit
>    else
>        don't do anything at all
>    end if
>If done sequentially according to the way you expect them to come out, it
>should short circuit itself just fine right???
>
>Signed : potentially and partially confused in Oregon
>

To understand short circuiting one have to understand boolean algebra.
consider the 3 boolean operators: NOT, AND, OR

each operator as a truth table as follow:

0 means FALSE   1 means TRUE


the NOT operator inverse it's input so:

IN |OUT
---|------
 0 |  1
 1 |  0


AND operator  truth table

  IN   |  OUT
A   B  |
-------------
0   0  |   0
0   1  |   0
1   0  |   0
1   1  |   1


as you see from this table a "AND" expression to be TRUE must have "A" and "B" 
TRUE so if after evaluating the first argument you find it FALSE then there is no
point evaluating the second argument  as you already know that the output will be
FALSE,  you can skip over this evalution (short circuiting)
But if argument "A" is TRUE you must evaluate "B" to know the answer.

OR operator truth table:

  IN   |  OUT
A   B  |
-------------
0   0  |   0
0   1  |   1
1   0  |   1
1   1  |   1

For "OR" operator if the first argument is TRUE you don't need to evaluate the
second argument as is truthness won't affect the result.  But if "A" is FALSE
you have to evaluate "B" to know the answer.

so in the following euphoria expression:

if sequence(a) AND  length(a)>0 then
  ...
  ...
end if

if the first argument "sequence(a)" return FALSE  there would be no need to
evaluate the second argument "length(a)>0" you already knows that the
if then won't be executed.

But in euphoria you can't write such an expression, because it always evaluate
both arguments so if "a" is an atom then you get a fatal error.  if euphria was
short circuiting the it would'n result in a fatal error because "length(a)>0"
would not be executed if "a" was an atom.

The consequence is that you have to write if statement inside if statement.

if sequence(a) then
  if length(a) > 0 then
  ...
  ...
  end if
end if

I think that most of us, excepting  Ralf blink  would prefect the first writing
to the second.






Jacques Deschenes
Baie-Comeau, Quebec
Canada
desja at globetrotter.qc.ca

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu