Icon (programming language)

The success and failure of a given block of code is used to direct further processing, whereas conventional languages would typically use Boolean logic written by the programmer to achieve the same ends.

Because the logic for basic control structures is often implicit in Icon, common tasks can be completed with less explicit code.

Icon was designed by Ralph Griswold after leaving Bell Labs where he was a major contributor to the SNOBOL language.

[3] The original SNOBOL effort, retroactively known as SNOBOL1, launched in the fall of 1962 at the Bell Labs Programming Research Studies Department.

[4] The effort was a reaction to the frustrations of attempting to use the SCL language for polynomial formula manipulation, symbolic integration and studying Markov chains.

SCL, written by the department head Chester Lee, was both slow and had a low-level syntax that resulting in volumes of code for even simple projects.

This led almost immediately to SNOBOL2, which added a number of built-in functions, and the ability to link to external assembly language code.

[6] SNOBOL3's introduction corresponded with major changes within the Bell Labs computing department, including the addition of the new GE 645 mainframe which would require a rewrite of SNOBOL.

Instead, the team suggested writing a new version that would run on a virtual machine, named SIL for SNOBOL Intermediate Language, allowing it to be easily ported to any sufficiently powerful platform.

[7] Further work on the language continued throughout the rest of the 1960s, notably adding the associative array type in later version, which they referred to as a table.

Additionally, control structures were almost entirely based on branching around code rather than the use of blocks, which were becoming a must-have feature after the introduction of ALGOL 60.

[11] Griswold began the effort of implementing SNOBOL's underlying success/failure concept with traditional flow control structures like if/then.

On the other hand, Icon uses C-style braces for structuring execution groups, and programs start by running a procedure called main.

[16] One of the key concepts in SNOBOL was that its functions returned the "success" or "failure" as primitives of the language rather than using magic numbers or other techniques.

[17][18] For example, a function that returns the position of a substring within another string is a common routine found in most language runtime systems.

[22] Icon's branching and looping constructs are all based on the success or failure of the code inside them, not on an arbitrary Boolean test provided by the programmer.

[18] In traditional language, these "other conditions" have no natural way of being indicated; additional magic numbers may be used, but more typically exception handling is used to "throw" a value.

As this is not a test per se, but an operator that returns a value, they can be strung together allowing things like if a < b < c,[22] a common type of comparison that in most languages must be written as a conjunction of two inequalities like if (a < b) && (b < c).

A key aspect of goal-directed execution is that the program may have to rewind to an earlier state if a procedure fails, a task known as backtracking.

[24] Converting a procedure to be a generator uses the suspend keyword, which means "return this value, and when called again, start execution at this point".

Functions will not be called unless evaluating their parameters succeeds, so this example can be shortened to: Internally, the alternator is not simply an or and one can also use it to construct arbitrary lists of values.

This can be used to iterate over arbitrary values, like: As lists of integers are commonly found in many programming contexts, Icon also includes the to keyword to construct ad hoc integer generators: which can be shortened: Icon is not strongly typed, so the alternator lists can contain different types of items: This writes 1, "hello" and maybe 5 depending on the value of x.

For instance, the above loop can be re-written this way:[24] In this case, the values from i to j will be injected into someFunction and (potentially) write multiple lines of output.

In the common cases when multiple functions are being called on a single string in sequence, this style can significantly reduce the length of the resulting code and improve clarity.

upto is essentially the reverse of many; it returns the location immediately prior to its provided Cset, which the example then sets the &pos to with another tab.

This example can be made more robust through the use of a more appropriate "word breaking" Cset which might include periods, commas and other punctuation, as well as other whitespace characters like tab and non-breaking spaces.

Laurence Tratt wrote a paper on Icon examining its real-world applications and pointing out a number of areas of concern.

This could be dismissed as one of those "gotchas" that programmers have to be aware of in any language, but Tratt examined a variety of Icon programs and found that the vast majority of procedures are not generators.

This means that Icon's default behaviour is only used by a tiny minority of its constructs, yet represents a major source of potential errors in all the others.

While the success/fail system works in most cases where the ultimate goal is to check a value, this can still lead to some odd behaviour in seemingly simple code:[25] This program will print "taken".