Icon (programming Language) - Goal-directed Execution

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:

    Some hours seem not to be occasion for any deed, but for resolves to draw breath in. We do not directly go about the execution of the purpose that thrills us, but shut our doors behind us and ramble with prepared mind, as if the half were already done. Our resolution is taking root or hold on the earth then, as seeds first send a shoot downward which is fed by their own albumen, ere they send one upward to the light.
    Henry David Thoreau (1817–1862)