Flow-control structures: Difference between revisions

m
Unicon/Icon consistency 2 + next to do find somewhere for all the supporting detail here, but where? Language page?
m (Unicon/Icon consistency 2 + next to do find somewhere for all the supporting detail here, but where? Language page?)
Line 375:
9 ALARM( 999 ) ! quit HicEst immediately</lang>
 
=={{header| Icon}} and Unicon ==
==={{header|Icon}}===
 
====Prelude about Goal-Directed Evaluation and Generators====
A central feature of Icon and Unicon is what is known as Goal-Directed Evaluation, and the intimately related concept of Generators. Without trying to be a tutorial, the idea is that expressions can yield more than one result (Generators) and if a further part of the expression results in failure, the earlier Generators will be driven to yield more results. The effect is not unlike the backtracking found in Prolog or Regular Expressions, however the feature is built into the very core of the language. Prolog programmers will find it very familiar but of course with differences because Icon and Unicon do not use the functional language pattern matching technique of Prolog.
Line 383:
 
Another way of looking at it is to understand that every expression can yield a ''result sequence'' that can be empty; any code using this expression may choose to ask for more results, gather them into a container or aggregate, or choose to use one value and then move on without asking for all possible results.
====goto====
Does not exist in the Icon or Unicon language.
====next====
Restarts the enclosing loop. The conditional on the loop is evaluated as normal.
====break expr====
Default value of ''expr'' is the null value ''&amp;null''. This operator breaks out of the enclosing loop, yielding the expression as the result of the loop. Normally loops yield a failure ie no result, so you can write code like this:
<lang icon>
Line 405:
</lang>
breaks out of two levels of loop and re-enters the top of the third-level enclosing loop.
====return expr====
Default value of expr is ''&amp;null''. Apart from the usual meaning of ''return'', if the ''expr'' value fails, then the procedure actually fails too, ie does not yield a value. See description of ''fail'' keyword. If the ''expr'' is capable of yielding more than one result, only the first result is asked for and used.
====fail====
Causes the the enclosing procedure to terminate without returning value. This is different from returning void or a null value that many other languages do when the code does not return an actual value. For example, in
<lang icon>
Line 413:
</lang>
The value of x will not be replaced if ftn() issues the ''fail'' command. If ftn fails, then Goal-Directed Evaluation will also fail the assignment, therefore x is not assigned a new value. If the flow of control through a procedure falls off the end, the procedure implicitly fails.
====suspend expr====
Default value of expr is ''&amp;null''. Any procedure containing the ''suspend'' command will yield a value to the calling code. However the procedure remains in a state of suspended animation ready to be reactivated if the calling code demands another result due to Goal Directed Evaluation. Note that this capability is built directly into the runtime rather than being an artifically constructed behaviour provided by Python or C#'s use of the 'yield' keyword. Every and all expressions may suspend or be involved in a suspending expression without any effort. Behaviourally much closer to Prolog which also supports backtracking as a core part of the language. If the ''expr'' is capable of yielding more than one result, then supend (if driven) will progressively yield all of those values.
 
A procedure can contain several uses of ''suspend'' and it's quite reasonable for the procedure to execute many of them in any chosen order.
====stop(expr)====
Terminate program with prejudice.
====error trapping====
The keyword &amp;error is normally zero, but if set to a positive value, this sets the number of fatal errors that are tolerated and converted to expression failure; the value of &amp;error is decremented if this happens. Therefore the now-common TRY-CATCH behaviour can be written as:
<lang icon>
Line 432:
</lang>
Various idiomatic simplifications can be applied depending on your needs.
====error throwing====
Errors can be thrown using the function
<lang icon>
runerr(errnumber, errorvalue) # choose an error number and supply the offending value
</lang>
==={{header|Unicon}}===
This Icon solution works in Unicon.
 
=={{header|IDL}}==
Anonymous user