Goal-directed Execution
One of Icon's key concepts is that control structures are based on the "success" or "failure" of expressions, rather than on boolean logic, as in most other programming languages. Under this model, simple comparisons like if a < b
do not mean "if the operations to the right evaluate to true" as they would under most languages; instead it means something more like "if the operations to the right succeed". In this case the < operator succeeds if the comparison is true, so the end result is the same. In addition, the < operator returns its second argument if it succeeds, allowing things like if a < b < c
, a common type of comparison that in most languages must be written as a disjunction of two inequalities like if a < b && b < c
.
The utility of this concept becomes much clearer when you consider real-world examples. Since Icon uses success or failure for all flow control, this simple code:
if a := read then write(a)
will copy one line of the standard input to standard output. What's interesting about this example is that the code will work even if the read causes an error, for instance, if the file does not exist. In that case the statement a := read
will fail, and write will simply not be called.
Success and failure are passed "up" through functions, meaning that a failure inside a nested function will cause the functions calling it to fail as well. For instance, we can write a program to copy an entire input file to output in a single line:
while write(read)
When the read command fails, at the end of file for instance, the failure will be passed up the chain and write will fail as well. The while, being a control structure, stops on failure, meaning it stops when the file is empty. For comparison, consider a similar example written in Java-based pseudocode:
try { while ((a = read) != EOF) { write(a); }
} catch (Exception e) { // do nothing, exit the loop
}
In this case there are two comparisons needed, one for end of file (EOF) and another for all other errors. Since Java does not allow errors to be compared as logic elements, as under Icon, the lengthy try/catch
syntax must be used instead. Try blocks also impose a performance penalty for simply using them, even if no error occurs, a distributed cost that Icon avoids.
Icon refers to this concept as goal-directed execution, referring to the way that execution continues until some goal is reached. In the example above the goal is to read the entire file; the read command continues to succeed while there is more information to be read, and fails when there isn't. The goal is thus coded directly in the language, instead of using statements checking return codes or similar constructs.
Read more about this topic: Icon (programming Language)
Famous quotes containing the word execution:
“It is clear that in a monarchy, where he who commands the exceution of the laws generally thinks himself above them, there is less need of virtue than in a popular government, where the person entrusted with the execution of the laws is sensible of his being subject to their direction.”
—Charles Louis de Secondat Montesquieu (16891755)