Short-circuit evaluation: Difference between revisions

(Logo)
Line 118:
</pre>
== Icon and Unicon ==
The entire concept of using 'boolean' values for logic control runs counter to the philosophy of Icon. WhileInstead thisIcon taskhas couldsuccess be(something writtenthat literally,returns ita wouldresult) beand morefailure beneficialwhich tois showreally howa ansignal. Icon programmerThe wouldconcept approachis thesimilar sameto problemthat used in [[:Category:SNOBOL4|SNOBOL4]] and [[:Category:Lisp|Lisp]]. IconThere alreadyis embracesno theway ideato ofpass shortaround circuita evaluation'false' andvalue goesin furtherthat withsense. theBecause abilityfailure ofis expressionsa tosignal generatecontrol alternateis resultsalways onlyevaluated ifin needed.a short-circuit Formanner. moreOne informationconsequence seeof [[Icon%2BUnicon/Intro#Program_Flow_and_Control|Failurethis is that an option,expression Everything"i Returns< aj" Valuedoesn't Exceptreturn whena itboolean Doesn'tvalue, andinstead Goal-Directedit Evaluationreturns andthe Generators]]value of j. ConsequentlyWhile somethis smallmay libertiesseem willodd beat takenfirst withit thisallows for elegant expressions like "i < j < k". task:
 
While this task could be written literally, it would be more beneficial to show how an Icon programmer would approach the same problem. Icon already embraces the idea of short circuit evaluation and goes further with the ability of expressions to generate alternate results only if needed. For more information see [[Icon%2BUnicon/Intro#Program_Flow_and_Control|Failure is an option, Everything Returns a Value Except when it Doesn't, and Goal-Directed Evaluation and Generators]]. Consequently some small liberties will be taken with this task:
* For false we will use the null value &null and true we will use anything else (a 1 will do).
* Short-circuit evaluation uses success (a result) and failure (a signal that cannot be ignored and no result) so strictly speaking the boolean false will not be returned (only the failure signal).
Anonymous user