Cached at:
06/11/26, 01:38 PM
# A Brief Introduction to Icon
Source: [https://www2.cs.arizona.edu/icon/intro.htm](https://www2.cs.arizona.edu/icon/intro.htm)
Reprinted fromPreprints of the Second ACM SIGPLAN History of Programming Languages Conference \(HOPL\-II\), SIGPLAN Notices**28**, 3 \(1993\), 359\-360\.
Icon is a very high\-level imperative language with a rich repertoire of string and structure processing facilities\. It is available on a wide range of computers and is in wide use\.
In Icon, values, not variables, are typed\. Built\-in data types include numerics, character sets, strings, sets, lists, associative tables, records, and procedures\. The aggregate types \- sets, lists, tables, and records \- can hold values of any type\. Tables can be indexed by values of any type\. Numerics, character sets, and strings are atomic values; operations on them produce new values\. Aggregates use pointer semantics; operations on them can change existing values as well as produce new ones\. Strings and aggregates can be of arbitrary size, and their sizes can change during execution\. Memory management is automatic\.
Icon has an expression\-oriented syntax; even control structures are expressions\. Procedures consist of zero or more expressions separated by newlines or semicolons\. Icon programs consist of one or more procedure definitions, and execution begins by calling the procedure namedmain\.
Expressions can produce zero or more values\. As in traditional languages, many Icon expressions produce a single value, for example,x\+5produces the sum ofxand5\. Other expressions can produce more than one value, for example,x\|5producesxthen, if necessary, produces5\. Such expressions are called*generators*; there are several built\-in generators and procedures can be written to be generators\. Other examples of built\-in generators includextoy, which generates the integers fromxtoy, and\!x, which generates all the characters from stringxor the elements from aggregatex\.
A \`goal\-directed' expression evaluation mechanism is Icon's most distinguishing characteristic\. Expression evaluation seeks \`success' \- at least one value for an expression\. An expression \`fails' if it does not produce a value\. The evaluation mechanism tries all combinations of values from generators in pursuit of a successful outcome\. For example,y<\(x\|5\)first comparesytox\. Ifyis less thanx, evaluation succeeds and produces the valuex\. Ifyis not less thanx,yis compared to5, the next value generated by the subexpressionx\|5\. Ifyis less than5, evaluation succeeds and produces5\. Otherwise, evaluation fails, and no value is produced\. Comparison operators produce the value of their right operand if they succeed\.
Failure drives control expressions and inhibits subsequent evaluation\. For example,max:=max<xsetsmaxtoxonly when it is less thanx\. Likewise,
if y < \(x \| 5\) then write\("y=", y\)
prints the value ofyonly if it is less thanxor5\. The evaluation mechanism pervades Icon; for example, procedures are called only if all of their arguments succeed, so the last example could be written more succinctly as
write\("y=", \(x \| 5\) \> y\)
The backtracking implied by the goal\-directed evaluation mechanism is limited to the expression in which it occurs\. For example, in
max := max < x write\("y=", \(x \| 5\) \> y\)
failure in the second expression does not affect the outcome of the first\.
The expressionevery*e*1do*e*2 \`drives'*e*1 \- it evaluates*e*2 for every successful outcome of*e*1\. So, ifpis a list of 100 elements,
every i := \(1 to 10\) \| \(91 to 100\) do write\(p\[i\]\)
prints the first and last ten elements ofp\. Thedoclause is optional, and the evaluation mechanism often helps eliminate temporary variables likei, for example,
every write\(p\[\(1 to 10\) \| \(91 to 100\)\]\)
also prints the first and last ten elements ofp, and
every \!p := 0
sets each element ofpto zero\.
Icon has numerous built\-in procedures and operators that help analyze strings\. Many of these take strings and positions as operands and return strings or positions\. Positions denote locations*between*characters\. Strings can be analyzed by manipulating positions explicitly, but Icon's*string scanning*facility eliminates the need for most explicit positions\. The expression*s*?*e*establishes*s*as the subject to which string processing operations in*e*apply\. The expression*e*typically includes string analysis operations, but may include*any*operation\. An example of a string analysis operation isfind\(*s*\), which produces the positions in the subject at which*s*occurs as a substring\. For instance, if line is
"a fish is a fish is a fish"
then
every line ? write\(find\("fish"\)\)
prints3,13, and23\.
Another example ismove\(*i*\), which advances the position by*i*characters\. If the advancement is successful,movereturns the*i*\-character substring between the initial and final positions\. For example,
t := "" line ? while t := t \|\| move\(1\) \|\| "\."
setstto a string containing the characters of line followed by periods;\|\|denotes string concatenation\.whileevaluatest:=t\|\|move\(1\)\|\|"\."repeatedly until it fails, which occurs whenmove\(1\)is invoked at the end of the subject string\.
moveis a*matching function*because it returns the substring of the subject that is \`matched' as a result of changing the position\. Another matching function istab\(*i*\), which moves to position*i*in the subject and returns the substring between the old and new positions\.
upto\(*s*\)returns the position of any of the characters in*s*, andmany\(*s*\)returns the position following the longest possible substring containing only characters in*s*starting at the current position\. It is important to note that functions like many return positions, but the specific values of those positions are rarely important\. Positions are used most often as arguments to matching functions liketab\. For example, the following function generates the \`words' in its argument\.
```
procedure getword(str)
str ? while tab(upto(&letters)) do {
word := tab(many(&letters))
suspend word
}
end
```
&letterscontains all of the upper\- and lowercase letters\. The expressiontab\(upto\(&letters\)\)advances the position up to the next letter, andtab\(many\(&letters\)\)matches the word and assigns it toword\.suspendis likereturn, but suspends the invocation so that it can be*resumed*for alternate values, if necessary\. When the invocation is resumed, it continues where thesuspendleft off, so subsequent resumptions return the remaining words\. Thewhileterminates whentab\(upto\(&letters\)\)fails because there are no more words instr\.
An example of usinggetwordis \`common words,' a program that lists the most commonly used words in its input and their frequencies of occurrence\. The line numbers are for explanatory purposes and are not part of the program\.
```
1 procedure main(args)
2 k := integer(args[1]) | 10
3 words := table(0)
4 while line := read() do every words[getword(line)] +:= 1
5 words := sort(words, 4)
6 every 1 to k do write(pull(words), "\n", pull(words))
7 end
```
This program prints thekmost commonly used words\. Line 2 setskto the command\-line argument, if there is one and it is a legal integer, or to10\. Line 3 assigns an associative table towords; the argument totablespecifies each entry's initial value\. This table accumulates the frequency counts\. Line 4 reads in lines and usesgetwordto generate the words in each line, which indexwords\. The operator\+:=increments its left operand\.sort\(words,4\)in line 5 returns the contents of the tablewordsin a list sorted in increasing order by the frequency counts\. Indices and their count values alternate in this list, which is assigned towords\. Line 6 prints the counts and words for thekmost frequently used words bypulling them off the end of the list, which also shortens the list\.pullfails when the list is empty, in case there are notkwords\.
This example typifies Icon programs\. Most are much shorter than equivalent programs in traditional languages, for example, compare this 13\-line Icon program with an equivalent program written in the C programming language \([*Comm\. ACM*30, 594\-599, 1987](https://www.researchgate.net/publication/243781460_Literate_Programming_Printing_Common_Words)\)\.
---
\-\-[David R\. Hanson](https://sites.google.com/site/drhanson/),[Google](http://www.google.com/)