You are on page 1of 7

Icon programming language quick reference

General conventions

Icon programming language quick reference General conventions The comment symbol is # ; comments extend to

The comment symbol is #; comments extend to the end of the line.

Numeric constants can be written in exponential notation (e.g, 1.7e-4 for 1 7 10 4 ) or in the radix notation <radix>r<constant>, e.g., 16r0D0A for hexadecimal 0D0A.

String constants are written between double quotes (" "). Escapes include: \b backspace, \e escape, \f formfeed, \l linefeed, \n newline, \r return, \t horizontal tab, \’ single quote, \" double quote, \ for octal code , and \x for hexadecimal code .

Variable names must start with a letter, followed by letters, digits, or underbar (_). They can be any length.

Built-in types

Fundamental types integer:

Signed integers.

They may have many digits (thousands, even) but

extended-precision arithmetic can be slow.

real: Real numbers.

 

string:

Strings of characters.

Any character can be stored in a string, including

nulls.

cset: An improper subset of the character set.

Structural types

file: An open file handle.

list: A list of zero or more values of any type or mixture of types. Written as: [ 1 ,

2 ,

,

n ]

null: The type of variables to which no value is assigned.

procedure: An Icon procedure.

table:

An associative array, containing a set of ordered pairs ( ) where each is

a unique key value and is any value associated with that key.

co-expression: See the manual.

Reserved words

break by case create default do else end every fail global if initial link local next not of procedure record repeat return static suspend then to until while

Declarations

global

<name>,

record

<name>{<field>,

}#

Create

with:

var

:=

<name>()

local

<name>,

static

<name>,

Control structures

 

break

Exit the enclosing loop and produce e Produces the i whose i matches

case

of{

1

2

:

:

1

2

default:

}

create

every

fail

if

next

not

repeat

return

suspend

until

while

1

do

1

then

1

1

do

do

1

|

2

|

\

?

d

2

2

2

2

[else

3 ]

“Preprocessor” features

Create a co-expression for Iterate over outcomes of 1 Fail out of the current procedure Produces 2 if 1 succeeds, else 3 Go to the top of the loop Fails if succeeds Repeat forever (or until break) Return to the caller Suspend procedure, return Loop while 1 is false Loop while 1 is true Outcome of 1 followed by outcome of 2

Produce e forever Produce no more than results from Establish as &subject, evaluate

$define

Define a textual substitution

$else

Else construct for $ifdef and $ifndef

$endif

End of conditional compilation

$error

Signal a fatal compilation error

$ifdef

Conditional compilation when is defined

$ifndef

Conditional compilation when is undefined

$include

 

" "

Include file

$undef

Undefine a name

Operators

Precedence

(

)

{ ; }

[ , ]

Highest precedence

[

]

(

)

All unary operators

\

@

!

^

*

/

%

**

+

-

++

--

||

|||

All relationals

|

to

[by

]

 

:=

:=

 

where is any binary operator

?

&

;

,

Lowest precedence

Unary operators

 

+

Numeric value of

-

Negative of

?

Pseudorandom number [1 ] for integers, [0 1)

?

for 0 Randomly selected element of

~

 

Set complement of cset

=

tab(match( ))

!

Sequence of elements from

@

Outcome of activating co-expression

^

 

Refresh co-expression

*

Size of

.

Value of

/

Is undefined?

\

Is defined?

Binary operators

 

+

-

*

/

Ambition, distraction, uglification, derision

%

Remainder

 

^

 

b

<

<=

=

>=

>

~=

Numerical comparison

 

++

Set union

--

Set difference

**

Set intersection

||

String concatenation

<<

<<=

==

>>=

>>

~==

Lexical comparison

|||

 

Concatenate two lists

@

Activate co-expression , transmit to it

 

:=

Assignment

<-

Reversible assignment

:=:

Exchange

<->

Reversible exchange

===

~===

Value comparison

&

Produces y

. ?

:=

Structure field reference Same as := Establish as subject within

Built-in functions

Numeric functions

abs(

)

acos(

)

asin(

)

atan( ,

)

dtor(

)

exp(

)

cos(

)

iand(

,

)

icom(

)

, ishift( ,

)

ior(

)

ixor(

,

)

log(

,

)

rtod(

)

sin(

)

sqrt(

)

tan(

)

String functions

any( ,

,

l ,

center(

,

,

r ,

)

,

entab( , 1 , 2 ,

,

,

n )

n )

bal( ,

, )

detab( , 1 , 2 ,

)

,

find( , left( ,

, ,

,

)

,

)

)

many( , map( ,

, , )

Absolute value of integer or real Arccos, returns radians Arcsin, returns radians Arctan, default 2nd arg is 1 Degrees to radians Exponential Argument in radians Bitwise and Bitwise not (one’s complement) Bitwise or Shift left ; negative for right shift Bitwise exclusive or log b ; default is Radians to degrees Argument in radians Square root Argument in radians

Fails if [ ] , else produces Balances l , r up to Centers in field of , padded with Untabify; default (,9) Tabify Find target in source

Flush-left in field of , padded with Scan while

Map characters from to in .

E.g., to upshift:

map( ,

&lcase,

&ucase)

match(

,

,

,

)

Is target a prefix of ?

move(

)

Advance &pos by , return part skipped

pos(

)

Is &pos at ?

repl(

,

)

Replicate copies of

reverse( ) right( ,

,

)

Reverse string Flush right in field of width , pad

tab(

)

Set &pos to , return string from old &pos to there

trim(

,

)

Trim trailing characters

upto(

,

,

,

)

Scan s until a character

Structural functions

char( )

Character whose ordinal is

copy(

)

Structure copy

cset(

)

Cast to cset

delete( get( )

,

)

Delete element from set Pop first element of list

image(

)

String image of

, integer( )

insert(

)

Insert into set Cast to integer

list( , member( , numeric( ) ord( ) pop( ) pull( )

)

)

List of elements equal to Set membership: is ? Cast to integer or real Ordinal of 1-character strings Pop first element of list Pop last element of list

push( ,

)

)

)

Push as first element of

put( ,

Push as last element of

real( ) seq( , ) set( ) sort( ) sort( ,

Cast to real Infinite sequence: + + 2 Set of distinct elements in list Sort a list Sort table . Produces a list of 2-elt lists ( , ) of indices and values; sort by if = 1, by if = 2.

sortf(

,

)

Sort lists and records, using the th field as key

string(

)

Cast to string

table(

)

New table with initial value

type(

)

String describing the type of

System functions

chdir(

)

Change directory to s

close( )

Close file

delay(

)

Delay i milliseconds

display( , exit( ) flush( ) function() getch() getche() getenv( ) kbhit()

)

Stack traceback with locals, levels Terminate with status Flush output to file Generates names of all builtin functions Get next character Get next character, no echo Value of environmental variable Is there a character available for getch()?

open( , ) Open a file: options taken from r (read), w (write), a (append), b (read/write), c (create), t (enable line terminator translation), and u (inhibit line terminator translation).

Name of variable Return next line from Read next characters from , may be short, fails only if no characters remain Delete file whose name is Rename file o as n Set file position, origin 1; negative = before end Write the expressions and stop with exit status 1 Execute a command line Returns the variable named Returns a file position Write expressions, then newline Write expressions, no newline

name( ) read( ) reads( ,

)

remove( )

rename( o ,

seek( ,

)

n )

stop( 1 , system( ) variable( ) where( ) write( 1 , writes( 1 ,

,

n )

2 , 2 ,

)

)

Other operations

to

1 , ( 1 , [ ] [ : ] [ +: ]

[ 2 ,

2 ,

0 (

[ -: ]

[ 1 ,

2 ,

by

)

)

]

]

Arithmetic progression generator The outcome of e 0 Procedure or function call ith element of s Elements i through j Elements i through i+j Elements i-j through i Form a list from expressions

Keywords (built-in variables)

&ascii

a cset of 7-bit ASCII

&clock

hh:mm:ss

&cset

Full character set, universe for csets

&date

yyyy/mm/dd

&dateline

Sunday, December 7, 1941 7:01 am

&digits

012 9

&e

2.71828

&errout

Standard error stream

&fail

Fails

&host

Host system identification

&input

Standard input stream

&letters

a zA Z

&lcase

a z

&level

Current procedure call level

&main

Co-expression for the initial call of main()

&null

The null value

&output

Standard output stream

&phi

Golden ratio , 1.61803

&pi

, 3.14159

&pos

Current scan position

&program

File name of executing program

&random

Pseudorandom seed

&source

Co-expression for the activator of the current co-

&subject

expression Current scan string

&time

Milliseconds of cpu time since program start

&trace

Controls procedure trace: 0 for none, -1 for

&ucase

A Z

&version

Current Icon version

Written by John W. Shipman (tcc-doc@nmt.edu). This version printed 2003-06-30. Copyright

Mexico Institute of Mining and Technology.

c 2001 by the New