Icon (programming language)

related topics
{math, number, function}
{system, computer, user}
{son, year, death}
{work, book, publish}
{line, north, south}

Icon is a very high-level programming language featuring goal directed execution and many facilities for managing strings and textual patterns. It is related to SNOBOL and SL5, string processing languages. Icon is not object-oriented, but an object-oriented extension called Idol was developed in 1996 which eventually became Unicon.


Basic syntax

The Icon language is derived from the ALGOL-class of structured programming languages, and thus has syntax similar to C or Pascal. Icon is most similar to Pascal, using := syntax for assignments, the procedure keyword and similar syntax. On the other hand, Icon uses C-style brackets for structuring execution groups, and programs start by running a procedure called "main".

In many ways Icon also shares features with most scripting programming languages (as well as SNOBOL and SL5, from which they were taken): variables do not have to be declared, types are cast automatically, and numbers can be converted to strings and back automatically. Another feature common to many scripting languages, but not all, is the lack of a line-ending character; in Icon, lines not ended by a semicolon get ended by an implied semicolon if it makes sense.

Procedures are the basic building blocks of Icon programs, and although they use Pascal naming they work more like C functions and can return values; there is no function keyword in Icon.

procedure doSomething(aString)

[edit] 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 cannot be directly stated in most languages.

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) {
} 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.

[edit] Generators

Expressions in Icon often return a single value, for instance, x < 5 will evaluate and succeed with the value 5 or fail. However several of the examples below rely on the fact that many expressions do not immediately return success or failure, returning values in the meantime. This drives the examples with every and to; every causes to to continue to return values until it fails.

This is a key concept in Icon, known as generators. Generators drive much of the loop functionality in the language, but do so more directly; the programmer does not write a loop and then pull out and compare values, Icon will do all of this for you.

Full article ▸

related documents
Kernel (matrix)
Square root
Tail recursion
Cholesky decomposition
Cantor's diagonal argument
Template (programming)
Equivalence relation
Extended Euclidean algorithm
Hausdorff dimension
Taylor's theorem
Standard ML
Complete metric space
Insertion sort
L'Hôpital's rule
Dirac delta function
Metric space
Vigenère cipher
Set (mathematics)
Exponential function
Abstraction (computer science)
Natural number
Analysis of algorithms
Semidirect product