Department of Computer
Science The University of Arizona Tucson, Arizona IPD266a March 2, 1996 http://www.cs.arizona.edu/icon/docs/ipd266.htm |
which succeeds (and produces the value ofi > j
j
) provided that
the value of i
is greater than the value of j
,
but fails otherwise. Similarly,
succeeds if the value ofi > j > k
j
is between i
and k
.which assigns the value ofif i > j then k := i else k := j
i
to k
if the value
of i
is greater than the value of j
, but assigns
the value of j
to k
otherwise.find(s1,s2)
, which fails if s1
does not occur
as a substring of s2
. Thus
writes the position at whichif i := find("or",line) then write(i)
"or"
occurs in line
,
if it occurs, but does not write a value if it does not occur.read(),
which produces the next line from the input file, but fails when the end
of the file is reached. The following expression is typical of programming
in Icon and illustrates the integration of conditional expressions and conventional
control structures:
This expression copies the input file to the output file.while line := read() do write(line)
do
clause in while-do
, the previous expression
can be rewritten as
while write(read())
Heresentence := "Store it in the neighboring harbor" find("or", sentence)
"or"
occurs in sentence
at positions
3, 23, and 33. Most programming languages treat this situation by selecting
one of the positions, such as the first, as the result of the expression.
In Icon, such an expression is a generator and is capable of producing all
three positions.which assigns the value 3 toi := find("or", sentence)
i
.Here the first result produced by the generator, 3, is assigned toif (i := find("or", sentence)) > 5 then write(i)
i
,
but this value is not greater than 5 and the comparison operation fails.
At this point, the generator is resumed and produces the second position,
23, which is greater than 5. The comparison operation then succeeds and
the value 23 is written. Because of the inheritance of failure and the fact
that comparison operations return the value of their right argument, this
expression can be written in the following more compact form:
Goal-directed evaluation is inherent in the expression evaluation mechanism of Icon and can be used in arbitrarily complicated situations. For example,write(5 < find("or", sentence))
succeeds iffind("or", sentence1) = find("and", sentence2)
"or"
occurs in sentence1
at the same position as "and"
occurs in sentence2
.every-do
control structure. An example is
which writes all the positions at whichevery i := find("or", sentence) do write(i)
"or"
occurs
in sentence
. For the example above, these are 3, 23, and 33.There are several built-in generators in Icon. One of the most frequently used of these isevery write(find("or", sentence))
which generates the integers fromi to j
i
to j
. This
generator can be combined with every-do
to formulate the traditional
for-style control structure:
Note that this expression can be written more compactly asevery k := i to j do f(k)
There are several other control structures related to generation. One is alternation,every f(i to j)
which generates the results ofexpr1 | expr2
expr1
followed by the results
of expr2
. Thus
writes the positions ofevery write(find("or", sentence1) | find("or", sentence2))
"or"
in sentence1
followed by the positions of "or"
in sentence2
.
Again, this sentence can be written more compactly by using alternation
in the second argument of find()
:
Another use of alternation is illustrated byevery write(find("or", sentence1 | sentence2))
which succeeds if any of(i | j | k) = (0 | 1)
i
, j
, or k
has the value 0 or 1.is a procedure that generates the odd-valued positions at whichprocedure findodd(s1, s2) every i := find(s1, s2) do if i % 2 = 1 then suspend i end
s1
occurs in s2
. The suspend
control structure returns
a value from the procedure, but leaves it in suspension so that it can be
resumed for another value. When the loop terminates, control flows off the
end of the procedure without producing another value.
wheres ? expr
s
is the subject string to be examined and expr
is an expression that performs the examination. A position in the subject,
which starts at 1, is the focus of examination.move(i)
,
moves the position by i
and produces the substring of the subject
between the previous and new positions. If the position cannot be moved
by the specified amount (because the subject is not long enough), move(i)
fails. A simple example is
which writes successive two-character substrings ofline ? while write(move(2))
line
, stopping
when there are no more characters.tab(i)
, which sets the position
in the subject to i
and also returns the substring of the subject
between the previous and new positions. For example,
first sets the position in the subject to 10 and then to the end of the subject, writing the remaining characters. Note that no value is written if the subject is not long enough.line ? if tab(10) then write(tab(0))
find()
can be used in string
scanning. In this context, the string that they operate on is not specified
and is taken to be the subject. For example,
writes all the substrings of line prior to occurrences ofline ? while write(tab(find("or"))) do move(2)
"or"
.
Note that find()
produces a position, which is then used by
tab()
to change the position and produce the desired substring.
The move(2)
skips the "or"
that is found.which writes all the words inline ? while tab(upto(&letters)) do write(tab(many(&letters)))
line
.in which the listcar1 := ["buick", "skylark", 1978, 2450]
car1
has four values, two of which are strings
and two of which are integers. Note that the values in a list need not all
be of the same type. In fact, any kind of value can occur in a list -- even
another list, as in
Lists also can be created byinventory := [car1, car2, car3, car4]
which creates a list ofL := list(i, x)
i
values, each of which has the value
x
.changes the last value incar1[4] := 2400
car1
to 2400. A reference that is
out of the range of the list fails. For example,
fails.write(car1[5])
L
are generated by !L
. Thus
writes all the values inevery write(!L)
L
.push(L,
x)
adds the value of x
to the left end of the list L
,
automatically increasing the size of L
by one. Similarly, pop(L)
removes the leftmost value from L
, automatically decreasing
the size of L
by one, and produces the removed value.
set()
.
Alternatively, set(L)
produces a set with the values in the
list L
. For example,
assigns toS := set([1, "abc", []])
S
a set that contains the integer 1, the string
"abc"
, and an empty list. member(S, x)
succeeds if x
is a member
of the set S
but fails otherwise. The function insert(S,
x)
adds x
to the set S
, while delete(S,
x)
removes x
from S
. A value only can occur
once in a set, so insert(S, x)
has no effect if x
is already in S
. !S
generates the members of S
.words := set() while line := read() do line ? while tab(upto(&letters)) do insert(words, tab(many(&letters))) every write(!words)
which assigns tosymbols := table(0)
symbols
a table with the default value 0.
The default value is used for keys that are not assigned another value.
Subsequently, symbols
can be referenced by any key, such as
which assigns the value 1 to the keysymbols["there"] := 1
"there"
in symbols
.Here the default value for each word is 0, as given inwords := table(0) while line := read() do line ? while tab(upto(&letters)) do words[tab(many(&letters))] +:= 1
table(0)
,
and +:=
is an augmented assignment operation used to increment
the values. There are augmented assignment operations for all binary operators.T
by the function sort(T,
1)
. The form of the list depends on the value of i
.
For example, if i
is 3, the list contains alternate keys and
their corresponding values in T
. For example,
writes the words and their counts from the tablewordlist := sort(words, 3) while write(pop(wordlist), " : ", pop(wordlist))
words
.
The program reads a line, writes it out with an identifying line number, and processes every word in the line. Words less than three characters long are considered to be "noise" and are discarded. A table,global uses, lineno, width procedure main(args) width := 15 # width of word field uses := table() lineno := 0 every tabulate(words()) # tabulate all citations output() # print the citations end # Add line number to citations for word # procedure tabulate(word) /uses[word] := set() insert(uses[word], lineno) return end # Generate words # procedure words() while line := read() do { lineno +:= 1 write(right(lineno, 6), " ", line) map(line) ? while tab(upto(&letters)) do { s := tab(many(&letters)) if *s >= 3 then suspend s# skip short words } } end # Print the results # procedure output() write() # blank line uses := sort(uses, 3) # sort citations while word := get(uses) do { line := "" numbers := sort(get(uses)) while line ||:= get(numbers) || ", " write(left(word, width), line[1:-2]) } end
uses
,
is keyed by the words. Every key has a corresponding set of line numbers.
The first time a word is encountered, a new set is created for it. The line
number is inserted in any event. Since a value can be in a set only once,
duplicate line numbers are suppressed automatically.the output isOn the Future!-how it tells Of the rapture that impells To the swinging and the ringing Of the bells, bells, bells- Of the bells, bells, bells, bells, Bells, bells, bells- To the rhyming and the chiming of the bells!
1 On the Future!-how it tells 2 Of the rapture that impells 3 To the swinging and the ringing 4 Of the bells, bells, bells- 5 Of the bells, bells, bells, bells, 6 Bells, bells, bells- 7 To the rhyming and the chiming of the bells! and 3, 7 bells 4, 5, 6, 7 chiming 7 future 1 how 1 impells 2 rapture 2 rhyming 7 ringing 3 swinging 3 tells 1 that 2 the 1, 2, 3, 4, 5, 7
Icon Project
Department of Computer Science
The University of Arizona
P.O. Box 210077
Tucson, AZ 85721-0077
U.S.A.
(520) 621-6613 (voice)
(520) 621-4246 (fax)
icon-project@cs.arizona.edu
The Icon Programming
Language, Prentice-Hall, Inc., Englewood Cliffs, NJ, second edition,
1990.
Version 9.3
of the Icon Programming Language, The Univ. of Arizona Icon Project
Document IPD278, 1995.
Graphics
Facilities for the Icon Programming Language; Version 9.3, The Univ.
of Arizona Icon Project Document IPD281, 1995.