# Icon Programming
## Introduction
### Icon - What is this?

Icon is a modern high-level programming language. It provides many useful features for managing data, generators and much more. It is not object-oriented, but the UN-icon variant, UNICON, is object-oriented as is ObjectIcon (code.google.com/p/objecticon)

### Hello world

Here is a tiny but complete Icon program.
As you see, the code is very simple. Lines procedure main and end enclose the body of the program. The second line is responsible for printing Hello world! on your standard output (usually screen). Naturally, procedure write prints the string which is given as a parameter. There is a similar procedure read which reads a line from standard input (usually keyboard).

### Compiling and running

Save the program from the previous section to file helloworld.icn. Type
in linux shell. The executable file named helloworld will be generated. You can run it now.

## Basic syntax

The program consists of procedures. Every procedure begins with procedure reserved word followed by the name of the procedure and list of arguments (it can be empty). The procedure ends with end. All the instructions are separated by newline or semicolon.
There must be a main procedure which is called automatically when user runs the program.
If you want to put a comment into your code, use # character. All the text in the line after that character is discarded by the compiler.

## Variables
### About variables in Icon

Variables in Icon have no type and can contain value of any data type. That means you can for example assign a number to one of your variables and assign a string to it few instructions later.
There is no need to declare variables (except global ones), they are created when they are used for the first time. There is no need to destroy them too, because there is a garbage collector doing it automatically.

### Assigning value to variable

To assign value to variable use Pascal-like := operator:

### Global variables
## Strings
### Introduction to strings in Icon

A string is a sequence of characters. You can easily put string constants into your code, enclosing them with pair of " characters. For example:
will create a string constant containing 8 characters and assign this string to str variable.

### Accessing characters and substrings

Characters in strings are indexed in a very convenient way. As in Pascal, 1 points to the place before the first character, 2 to the place between the first and the second etc. Unlike many other languages, strings are indexed from the end too. 0 points to the place after the last character, -1 to the place before the last character, etc.
To access the part of the string you want, use the brackets [ ] operator:
* variablename[from:to]
* variablename[index]
The first method returns a substring placed between points pointed by from and to. If you place only one number in the brackets, it is equivalent to variablename[index:index+1].

### Operators

* *str - produces the length of str
* ?str - produces the random one-character substring of str
* !str - generates the sequence of one-character substrings of str
* str1 || str2 - concatenates two strings:
* strings case-sensitively comparison:
** str1 == str2 - produces str2 if those strings are equal, fails otherwise
** str1 ~== str2 - produces str2 if those strings are not equal, fails otherwise
** str1 << str2 - produces str2 if it is greater than str1, fails otherwise
** str1 <<= str2 - produces str2 if it is greater or equal than str1, fails otherwise
** str1 >> str2 - produces str2 if it is less than str1, fails otherwise
** str1 >>= str2 - produces str2 if it is less or equal than str1, fails otherwise
* str ? expr - sets str as &subject for expr expression

### String scanning
## Lists
### Overview of lists

Lists are similar to strings, although they are able to contain much more various data. Like strings, lists are indexed from the beginning and from the end (read the Accessing characters and substrings section related to strings).

### List creation

You can create list constants enclosing its elements with brackets and separating them with , character. For example:
creates three-element list constant and assigns it to the lst variable. To create empty list do not put anything between brackets:
There is a special function for creating lists of desired length:
* list (i,x)
i-element list is created and all its elements are initialized with x value. By default i is 0 and x is &null.

### Arrays

There are no arrays in Icon. Use lists instead. If you need multi-dimensional arrays, use lists of lists.

### Stack and queue

Very useful functions are given to manage stacks and queues as lists.
* push(list, elem) - adds elem to the list list at its beginning.
* pop(list) - produces the first element of list and removes it from list or fails if list is empty.
* put(list, elem) - adds elem to the end of list.
* get(list) - produces the first element of list and removes it from list or fails if list is empty, it is identical to pop.
* pull(list) - produces the last element of list and removes it from list or fails if list is empty.

## Record types
### Record type declaration

To declare record type use record reserved word, for example:

### Construction of the record variable

When you declare a record, a constructor for it is defined automatically. A constructor is a procedure, which arguments are initial values for record fields and returns the created record. The constructor 's name is the same as record's.

### Operators

* rec.fieldname - produces a value of the fieldname field of the rec record
* *rec - produces a number of fields of the rec record
* ?rec - produces a value of a random field of the rec record
* !rec - generates values of all fields of the rec record

## Procedures

A procedure which forms part of the Icon language is conventionally referred to as a "function". A function which is added by the user as part of a program are referred to as a "procedure". A procedure may have a return value by preceding an identifier with the reserved word "return".

## Generators

Generators, which are so fundamental to newer languages such as Ruby and Python, were first used in ICON shortly after CLU.

## Goal-directed execution

Many ICON expressions are called "conditional expressions" and their evaluation results in success or failure. Three important examples are
Comparison operators form expressions which succeed or fail such as
If count is not greater than zero then the comparison in the conditional expression fails; without an expression, the enclosing expression which is a call to the function write, is never evaluated and in that sense, inherits the failure of the conditional expression. By way of comparison
Goal-directed execution is not the very basis of Icon as it is in , say, Prolog. Icon is rather more like Oz, where backtracking is an option. In Icon, backtracking is available, but within the limits of a particular context. ICON also has the concept of "data backtracking"

## External links

* Icon homepage
* Unicon homepage