Short-circuit evaluation: Difference between revisions
(→{{header|PureBasic}}: Added task) |
(+Icon) |
||
Line 17: | Line 17: | ||
y = a(j) or b(j) |
y = a(j) or b(j) |
||
If the language does not have short-circuit evaluation, this might be achieved with nested if statements. |
If the language does not have short-circuit evaluation, this might be achieved with nested if statements. |
||
== Icon and Unicon == |
|||
Placeholder. |
|||
==={{header|Icon}}=== |
|||
<lang Icon> |
|||
</lang> |
|||
==={{header|Unicon}}=== |
|||
<lang Unicon> |
|||
</lang> |
|||
The Icon solution works in Unicon. |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
Line 63: | Line 73: | ||
Calculating: x = a(1) Or b(1) |
Calculating: x = a(1) Or b(1) |
||
# Called function a(1)</pre> |
# Called function a(1)</pre> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Pythons '''and''' and '''or''' binary, infix, boolean operators will not evaluate their right-hand expression if the outcome can be determined from the value of the left-hand expression. |
Pythons '''and''' and '''or''' binary, infix, boolean operators will not evaluate their right-hand expression if the outcome can be determined from the value of the left-hand expression. |
Revision as of 17:56, 24 July 2010
Assume functions a and b return boolean values, and further, the execution of function b takes considerable resources and is to be minimised.
If we needed to compute:
x = a() and b()
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:
y = a() or b()
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 x 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
Task Description
The task is to 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:
x = a(i) and b(j) y = a(j) or b(j)
If the language does not have short-circuit evaluation, this might be achieved with nested if statements.
Icon and Unicon
Placeholder.
Icon
<lang Icon> </lang>
Unicon
<lang Unicon> </lang> The Icon solution works in Unicon.
PureBasic
Logical And & Or operators will not evaluate their right-hand expression if the outcome can be determined from the value of the left-hand expression. <lang PureBasic>Procedure a(arg)
PrintN(" # Called function a("+Str(arg)+")") ProcedureReturn arg
EndProcedure
Procedure b(arg)
PrintN(" # Called function b("+Str(arg)+")") ProcedureReturn arg
EndProcedure
OpenConsole() For a=#False To #True
For b=#False To #True PrintN(#CRLF$+"Calculating: x = a("+Str(a)+") And b("+Str(b)+")") x= a(a) And b(b) PrintN("Calculating: x = a("+Str(a)+") Or b("+Str(b)+")") y a(a) Or b(b) Next
Next Input()</lang>
Calculating: x = a(0) And b(0) # Called function a(0) Calculating: x = a(0) Or b(0) # Called function a(0) # Called function b(0) Calculating: x = a(0) And b(1) # Called function a(0) Calculating: x = a(0) Or b(1) # Called function a(0) # Called function b(1) Calculating: x = a(1) And b(0) # Called function a(1) # Called function b(0) Calculating: x = a(1) Or b(0) # Called function a(1) Calculating: x = a(1) And b(1) # Called function a(1) # Called function b(1) Calculating: x = a(1) Or b(1) # Called function a(1)
Python
Pythons and and or binary, infix, boolean operators will not evaluate their right-hand expression if the outcome can be determined from the value of the left-hand expression. <lang python>>>> def a(answer): print(" # Called function a(%r) -> %r" % (answer, answer)) return answer
>>> def b(answer): print(" # Called function b(%r) -> %r" % (answer, answer)) return answer
>>> for i in (False, True): for j in (False, True): print ("\nCalculating: x = a(i) and b(j)") x = a(i) and b(j) print ("Calculating: y = a(i) or b(j)") y = a(i) or b(j)
Calculating: x = a(i) and b(j)
# Called function a(False) -> False
Calculating: y = a(i) or b(j)
# Called function a(False) -> False # Called function b(False) -> False
Calculating: x = a(i) and b(j)
# Called function a(False) -> False
Calculating: y = a(i) or b(j)
# Called function a(False) -> False # Called function b(True) -> True
Calculating: x = a(i) and b(j)
# Called function a(True) -> True # Called function b(False) -> False
Calculating: y = a(i) or b(j)
# Called function a(True) -> True
Calculating: x = a(i) and b(j)
# Called function a(True) -> True # Called function b(True) -> True
Calculating: y = a(i) or b(j)
# Called function a(True) -> True</lang>
Pythons if expression can also be used to the same ends (but probably should not): <lang python>>>> for i in (False, True): for j in (False, True): print ("\nCalculating: x = a(i) and b(j) using x = b(j) if a(i) else False") x = b(j) if a(i) else False print ("Calculating: y = a(i) or b(j) using y = b(j) if not a(i) else True") y = b(j) if not a(i) else True
Calculating: x = a(i) and b(j) using x = b(j) if a(i) else False
# Called function a(False) -> False
Calculating: y = a(i) or b(j) using y = b(j) if not a(i) else True
# Called function a(False) -> False # Called function b(False) -> False
Calculating: x = a(i) and b(j) using x = b(j) if a(i) else False
# Called function a(False) -> False
Calculating: y = a(i) or b(j) using y = b(j) if not a(i) else True
# Called function a(False) -> False # Called function b(True) -> True
Calculating: x = a(i) and b(j) using x = b(j) if a(i) else False
# Called function a(True) -> True # Called function b(False) -> False
Calculating: y = a(i) or b(j) using y = b(j) if not a(i) else True
# Called function a(True) -> True
Calculating: x = a(i) and b(j) using x = b(j) if a(i) else False
# Called function a(True) -> True # Called function b(True) -> True
Calculating: y = a(i) or b(j) using y = b(j) if not a(i) else True
# Called function a(True) -> True</lang>