Allegheny College
Department of Computer Science
CS220 Programming Languages
Spring 2007
Laboratory Session 6

ICON

 


Table of Contents

  1. Introduction
  2. Compiling and Running
  3. Basic Syntax
  4. Simple Arithmetic
  5. Expressions
  6. Conditional Expressions
  7. Generators
  8. Procedures
  9. Sample Code
  10. Resources

Introduction

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:

  1. High-Level Programming Language
  2. Agile (scripting) Programming Language
  3. Successor to SNOBOL
  4. Developed by Ralph Griswold, at Bell Labs in the 1960's.
  5. Powerful string processing features.
  6. Powerful data structure processing features.
  7. Strongly typed (values, not variables have type).
  8. Goal-Directed Expression Evaluation Mechanism (backtracking)
  9. Expressions can produce sequences of results (generators)
  10. Storage Allocation & Garbage Collection are done automatically
  11.  

ICON Articles

  1. ICON--An Agile Programming Language
  2. A Brief Introduction to ICON
  3. An Overview of the ICON Programming Language; Version 9

Learning through comparison:

  1. 99 Bottles of Beer in Java
  2. 99 Bottles of Beer in C
  3. 99 Bottles of Beer in C Shell
  4. 99 Bottles of Beer in BASH Shell
  5. 99 Bottles of Beer in Fortran 77
  6. 99 Bottles of Beer in Perl
  7. 99 Bottles of Beer in ICON
Back to Top

Compiling and Running

To Compile and Run ICON programs please following the instructions below:

 

Back to Top

Basic Syntax

  1. All ICON files will have a ".icn" file extension.
  2. Every ICON procedure begins with the word "procedure" and concludes with the word "end".
  3. Every Icon program must contain a procedure with the name ``main'', and that procedure is where execution start
  4. ICON expressions end with a semi-colon or newline

    A Tutorial for The ICON Programming Language

    Using Strings in ICON

    Text Processing in ICON

    Using Data Structures in ICON

Back to Top

Simple Arithmetic

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.
    end
Back to Top

Expressions

There are two important properties of Icon expressions:

  1. Icon has no statements, just expressions.
  2. An expression is a sequence of operations that may produce zero or more results.

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 sum

In 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.

Back to Top

Conditional Expressions

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
  1. (Succeeds)  If i > j, k is assigned the value of i 
  2. (Fails)  Otherwise, k is assigned the value of j 
  3. 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 if string_1 does not occur as a substring of string_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()) 
Back to Top

Generators

Back to Top

 


Procedures

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

 

Back to Top

Sample Code

[click here for directory]

99bottles.icn 

          
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

 

Back to Top

Resources

  1. ICON Home Page (Univ. of Arizona)
  2. Documents for the ICON Programming Language
  3. References for the ICON Programming Language
  4. ICON Expressions & Precedence
  5. ICON Functions(Univ. of Arizona)
  6. ICON Datatypes(Univ. of Arizona)
  7. ICON Reserved Words(Univ. of Arizona)
  8. ICON Escape Sequences(Univ. of Arizona)
  9. ICON Keywords(Univ. of Arizona)
  10. ICON Opperations(Univ. of Arizona)
  11. The ICON Trick Bag
  12. A Tutorial for The ICON Programming Language

Back to Top

Author information goes here.
Copyright © 2001  [OrganizationName]. All rights reserved.
Revised: 02/09/05.