How to resolve the algorithm Short-circuit evaluation step by step in the Quackery programming language
How to resolve the algorithm Short-circuit evaluation step by step in the Quackery 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 Quackery programming language
Source code in the quackery programming language
[ say "evaluating "
]this[ echo cr ] is ident ( --> )
[ iff say "true"
else say "false" ] is echobool ( b --> )
[ swap iff drop done
times drop
false ]done[ ] is SC-and ( b n --> )
[ swap not iff drop done
times drop
true ]done[ ] is SC-or ( b n --> )
[ ident
2 times not ] is a ( b --> b )
[ ident
4 times not ] is b ( b --> b )
[ say "i = "
dup echobool
say " AND j = "
dup echobool
cr
[ a 1 SC-and b ]
say "result is "
echobool cr cr ] is AND-demo ( --> )
[ say "i = "
dup echobool
say " OR j = "
dup echobool
cr
[ a 1 SC-or b ]
say "result is "
echobool
cr cr ] is OR-demo ( --> )
true true AND-demo
true false AND-demo
false true AND-demo
false false AND-demo
cr
true true OR-demo
true false OR-demo
false true OR-demo
false false OR-demo
You may also check:How to resolve the algorithm Vector products step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Rate counter step by step in the Jsish programming language
You may also check:How to resolve the algorithm Blum integer step by step in the Phix programming language
You may also check:How to resolve the algorithm Playing cards step by step in the Forth programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the Arturo programming language