Short-circuit evaluation: Difference between revisions

Added Quackery.
m (syntax highlighting fixup automation)
(Added Quackery.)
Line 3,521:
Calculating: y = a(i) or b(j) using y = b(j) if not a(i) else True
# Called function a(True) -> True</syntaxhighlight>
 
=={{header|Quackery}}==
 
Quackery does not include short-circuit evaluation, but it can be added by use of meta-control flow words (words wrapped in reverse brackets such as <code>]done[</code>.) For details see: [https://github.com/GordonCharlton/Quackery/blob/main/The%20Book%20of%20Quackery.pdf The Book of Quackery]
 
The short-circuit evaluation words <code>SC-and</code> and <code>SC-or</code> defined here are used thus: <code>[ a 1 SC-and b ]</code> and <code>[ a 1 SC-or b ]</code>. These presumes that the arguments to <code>a</code> and <code>b</code> are on the stack in the order <code>j i</code>.
 
The <code>1</code> preceding the word is required to indicate the number of arguments that <code>b</code> would consume from the stack if it were evaluated.
 
Extending the task to three functions, the third, <code>c</code> also consuming one argument <code>k</code> and returning a boolean, present on the stack underneath <code>j</code> and <code>i</code> would lead to the code <code>[ a 2 SC-and b 1 SC-and c ]</code> and <code>[ a 2 SC-or b 1 SC-or c ]</code>, where the <code>2</code> is the sum of the number of arguments consumed by <code>b</code> and <code>c</code>, and the <code>1</code> is the number of arguments consumed by <code>c</code>.
 
<code>SC-and</code> and <code>SC-or</code> can both be used in a single short-circuit evaluation nest with three or more functions. Evaluation is strictly left to right.
 
Quackery does not have variables, so no assignment is shown here. Words (functions) leave their results on the stack. If desired results can be moved to ancillary stacks, which include standing-in for variables amongst their functionality.
 
<syntaxhighlight lang="Quackery">
[ 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</syntaxhighlight>
 
{{out}}
 
<pre>i = true AND j = true
evaluating a
evaluating b
result is true
 
i = false AND j = false
evaluating a
result is false
 
i = true AND j = true
evaluating a
evaluating b
result is false
 
i = false AND j = false
evaluating a
result is false
 
 
i = true OR j = true
evaluating a
result is true
 
i = false OR j = false
evaluating a
evaluating b
result is true
 
i = true OR j = true
evaluating a
result is true
 
i = false OR j = false
evaluating a
evaluating b
result is false</pre>
 
 
=={{header|R}}==
1,462

edits