How to resolve the algorithm Short-circuit evaluation step by step in the REXX programming language
How to resolve the algorithm Short-circuit evaluation step by step in the REXX programming language
Table of Contents
Problem Statement
Assume functions a and b return boolean values, and further, the execution of function b takes considerable resources without side effects, and is to be minimized. If we needed to compute the conjunction (and): Then it would be best to not compute the value of b() if the value of a() is computed as false, as the value of x can then only ever be false. Similarly, if we needed to compute the disjunction (or): Then it would be best to not compute the value of b() if the value of a() is computed as true, as the value of y can then only ever be true. Some languages will stop further computation of boolean equations as soon as the result is known, so-called short-circuit evaluation of boolean expressions
Create two functions named a and b, that take and return the same boolean value. The functions should also print their name whenever they are called. Calculate and assign the values of the following equations to a variable in such a way that function b is only called when necessary: If the language does not have short-circuit evaluation, this might be achieved with nested if statements.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Short-circuit evaluation step by step in the REXX programming language
Source code in the rexx programming language
/*REXX programs demonstrates short─circuit evaluation testing (in an IF statement).*/
parse arg LO HI . /*obtain optional arguments from the CL*/
if LO=='' | LO=="," then LO= -2 /*Not specified? Then use the default.*/
if HI=='' | HI=="," then HI= 2 /* " " " " " " */
do j=LO to HI /*process from the low to the high.*/
x=a(j) & b(j) /*compute function A and function B */
y=a(j) | b(j) /* " " " or " " */
if \y then y=b(j) /* " " B (for negation).*/
say copies('═', 30) ' x=' || x ' y='y ' j='j
say
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
a: say ' A entered with:' arg(1); return abs( arg(1) // 2) /*1=odd, 0=even */
b: say ' B entered with:' arg(1); return arg(1) < 0 /*1=neg, 0=if not*/
You may also check:How to resolve the algorithm Amicable pairs step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Ecstasy programming language
You may also check:How to resolve the algorithm Hamming numbers step by step in the Rust programming language
You may also check:How to resolve the algorithm Eertree step by step in the 11l programming language
You may also check:How to resolve the algorithm Extend your language step by step in the Plain TeX programming language