Allegheny College
Department of Computer Science
CS220 Programming Languages
Spring 2007
Laboratory Session 6
Icon is a high-level imperative language, with powerful features for parsing (string processing) and processing data structures. Additionally, ICON has a full set of computational facilities that make it a powerful tool for applications such as text analysis, text editing, document formatting, AI, expert systems, rapid prototyping, symbolic mathematics, text generation, and data laundry.
Important features of ICON:
ICON Articles
To Compile and Run ICON programs please following the instructions below:
A Tutorial for The ICON Programming Language
Here is a simple Icon program that finds the sum of the first 100 integers:
procedure main()
local n, sum # Variables are defined. Variable Type is determined by use.
sum := 0; # variable := expression
every n := 1 to 100 do # loop construct: every condition do expression
sum := sum + n; # ...add n to the sum
write ( "The sum of all numbers from 1 to 100 is ", sum ); #output string and sum variable to standard output.
endThere are two important properties of Icon expressions:
In general, Icon loops take the form ``every expressoin_1 do
expersoin_2'' . If expression_1 produces no results,
expersion_2 is never evaluated. If it produces 19 results,
expersion_2 is evaluated 19 times, and so on.
Let's take another look at the loop in the simple arithmetic program:
every n := 1 to 100 do # For n equal to 1, 2, 3, 4, 5 ...
sum := sum + n; # ...add n to the sumIn this case, the expression ``n:=1 to 100'' produces 100
results, so the loop is executed 100 times. The expression ``1 to 100''
produces the values from 1 to 100 in sequence.
Conditional Expressions are logical programming constructs that can either succeed or fail. If a conditional expression succeeds, a value is returned. Otherwise, the expression fails and no value is returned. Consider the simple conditional expression below:
a > b
If "a" is greater than "b" the expression succeeds and a is returned. Otherwise no value is returned. Likewise
a > b > c
succeeds if the value of "b" is between "a" and
"c".
The success or failure of conditional operations is used instead of Boolean
values to drive control structures
example:
if i > j then k := i else k := j
(Succeeds) If i > j, k is assigned the value of i
(Fails) Otherwise, k is assigned the value of j
How would you achieve the same results with C or Java?
The use of developing code based upon the concepts of success and failure is easier because it allows for the more concise code.
This is more easily illustrated through an example using the find() function.
find(string_1,string_2), fails ifstring_1does not occur as a substring ofstring_2. Otherwise, find returns the
position at wich the substring string_1 begins in string_2. Thus
if i := find("or",line) then write(i)
writes the position at which "or" occurs in
line,
if it occurs, but does not write a value if it does not occur.
Many expressions in Icon are conditional. An example is read(),
which writes 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:
while line := read() do { write(line)}
or
while write(read())
The form of a procedure declaration:
procedure name(parameter_1, parameter_2, ....)
declarations
initial_expression_option
expression_sequence
end
example 1:
procedure oct(i)
return if i = 0 then "0" else oct(i/8) || i % 8
end
procedure main()
i:=1
while x:= read() do{
write("#define ", x, " ", oct(i))
i +:=i
}
end
example 2:
procedure G()
suspend | writes(" e1")\3 do writes(" e2")
write()
suspend | writes(" e3")\2
write()
end
procedure main
every G() do writes(" e4")
end
helloworld.icn
string_subscripting.icn
Eight_Queens.icn
helloworld2.icn
date_map.icn
sum_1-00.icn
fibonacci_repeat.icn
fibonacci_until.icn
tcpsockets.icn
find_right_triangle.icn
local_declarations.icn
unititialized_variables.icn
hello.icn
repeat.icn
words.icn
string_contatenate.icn
write.icn