Icon programming language quick reference

General conventions
Numeric constants can be written in exponential notation (e.g, 1.7e-4 for 1X7 ¢ 10−4 ) or in the radix notation <radix>r<constant>, e.g., 16r0D0A for hexadecimal 0D0A. String constants are written between double quotes ("X X X "). Escapes include: \b backspace, \e escape, \f formfeed, \l linefeed, \n newline, \r return, \t horizontal tab, \’ single quote, \" double quote, \ddd for octal code ddd, and \xdd for hexadecimal code dd. Variable names must start with a letter, followed by letters, digits, or underbar (_). They can be any length. The comment symbol is #; comments extend to the end of the line.

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

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: [e1 , e2 , X X X , en ]  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 (kY v ) where each k is a unique key value and v 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
New Mexico Tech Computer Center Icon programming language quick reference Page 1

global <name>, X X X record <name>{<field>, X X X }# Create with: local <name>, X X X static <name>, X X X var := <name>()

Control structures
break e case n of{ x1 : e 1 x2 : e 2 XXX default: ed } create x every e1 do e2 fail if e1 then e2 [else e3 ] next not e repeat e return e suspend e until e1 do e2 while e1 do e2 e1 | e2 | e e \ n s ? e
Exit the enclosing loop and produce e Produces the ei whose xi matches n

Create a co-expression for x Iterate over outcomes of e1 Fail out of the current procedure Produces e2 if e1 succeeds, else e3 Go to the top of the loop Fails if e succeeds Repeat forever (or until break) Return e to the caller Suspend procedure, return e Loop while e1 is false Loop while e1 is true Outcome of e1 followed by outcome of e2 Produce e forever Produce no more than n results from e Establish s as &subject, evaluate e

“Preprocessor” features
$define n text $else $endif $error $ifdef n $ifndef n $include "f " $undef n
Define a textual substitution Else construct for $ifdef and $ifndef End of conditional compilation Signal a fatal compilation error Conditional compilation when n is defined Conditional compilation when n is undefined Include file f Undefine a name

New Mexico Tech Computer Center

Icon programming language quick reference

Page 2

Precedence (e) {e;X X X } [e,X X X ] e[e] e(e)
All unary operators Highest precedence

\ @ ! ^ * / % ** + - ++ -|| |||

e | e e to e [by e] := := ? & ; ,
Unary operators +n -n ?n ?x ~™ =s !x @e ^e *x .x /x \x Binary operators + - * / % —^˜ < <= = >= > ~= ++ -** || << <<= == >>= >> ~== ||| x @ e :=
New Mexico Tech Computer Center

All relationals

where  is any binary operator Lowest precedence

Numeric value of n Negative of n Pseudorandom numberP [1Y n] for integers, for n  0 Randomly selected element of x Set complement of cset ™ Sequence of elements from x Outcome of activating co-expression e Refresh co-expression e Size of x Value of x Is x undefined? Is x defined?

P [0Y 1)



Ambition, distraction, uglification, derision Remainder Numerical comparison Set union Set difference Set intersection String concatenation Lexical comparison Concatenate two lists Activate co-expression e, transmit x to it Assignment
Page 3

Icon programming language quick reference

<:=: <-> === ~=== x & y x.y x := y s?e

Reversible assignment Exchange Reversible exchange Value comparison Produces y Structure field reference Same as x := x  y Establish s as subject within e

Built-in functions
Numeric functions abs(x) acos(x) asin(x) atan(y , x) dtor(x) exp(x) cos(x) iand(—, ˜) icom(—) ior(x, y ) ishift(x, n) ixor(x, y ) log(x, ˜) rtod(x) sin(x) sqrt(x) tan(x) String functions any(™, s, i, j ) bal(™, ™l , ™r , s, i, j ) center(s, n, p) detab(s,i1 ,i2 ,X X X,in ) entab(s,i1 ,i2 ,X X X,in ) find(t, s, i, j ) left(s, n, p) many(™, s, i, j ) map(s, f , t) match(t, s, i, j ) move(n) pos(n) repl(s, n)
New Mexico Tech Computer Center

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 x left n; negative n for right shift Bitwise exclusive or logb x; default ˜ is e Radians to degrees Argument in radians Square root Argument in radians Fails if s[i]TP ™, else produces i Balances ™l , ™r up to ™ Centers s in field of n, padded with p Untabify; default (,9) Tabify Find target t in source s Flush-left s in field of n, padded with p Scan s while P ™ Map characters from f to t in s. E.g., to upshift:

map(s, &lcase, &ucase) Is target t a prefix of s? Advance &pos by n, return part skipped Is &pos at n? Replicate n copies of s

Icon programming language quick reference

Page 4

reverse(s) right(s, n, p) tab(n) trim(s, ™) upto(™, s, i, j ) Structural functions char(o) copy(x) cset(x) delete(s, x) get(v) image(x) insert(s, x) integer(x) list(n, x) member(s, x) numeric(x) ord(s) pop(v) pull(v) push(v, x) put(v, x) real(x) seq(i,j ) set(v) sort(—) sort(t, k ) sortf(x, k ) string(x) table(x) type(x) System functions chdir(s) close(f ) delay(i) display(i, f ) exit(i) flush(f ) function() getch() getche() getenv(s) kbhit()

Reverse string Flush right s in field of width n, pad p Set &pos to n, return string from old &pos to there Trim trailing characters P ™ Scan s until a character P ™ Character whose ordinal is o Structure copy Cast to cset Delete element x from set s Pop first element of list v String image of x Insert x into set s Cast to integer List of n elements equal to x Set membership: is x P s? Cast to integer or real Ordinal of 1-character strings Pop first element of list v Pop last element of list v Push x as first element of v Push x as last element of v Cast to real Infinite sequence: iY i + jY i + 2jY X X X Set of distinct elements in list v Sort a list Sort table t. Produces a list of 2-elt lists (i, v ) of indices and values; sort by i if k = 1, by v if k = 2. Sort lists and records, using the k th field as key Cast to string New table with initial value x String describing the type of x

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

New Mexico Tech Computer Center

Icon programming language quick reference

Page 5

open(n—me, options )

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 v Return next line from f Read next n characters from f , may be short, fails only if no characters remain Delete file whose name is s Rename file so as sn 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 s Returns a file position Write expressions, then newline Write expressions, no newline

name(v ) read(f ) reads(f , n) remove(s) rename(so , sn ) seek(f , p) stop(e1 , X X X, en ) system(s) variable(s) where(f ) write(e1 , e2 , X X X ) writes(e1 , e2 , X X X ) Other operations

i to j [ by k ] e0 (e1 , e2 , X X X) p(e1 , e2 , X X X) s[i] s[i:j ] s[i+:j ] s[i-:j ] [e1 , e2 , X X X]

Arithmetic progression generator The outcome of ee0 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 &clock &cset &date &dateline &digits &e &errout &fail &host &input &letters &lcase &level &main &null &output
New Mexico Tech Computer Center

a cset of 7-bit ASCII hh:mm:ss Full character set, universe for csets yyyy/mm/dd Sunday, December 7, 1941 7:01 am 012X X X 9 2.71828X X X Standard error stream Fails Host system identification Standard input stream aX X X zAX X X Z aX X X z Current procedure call level Co-expression for the initial call of main() The null value Standard output stream
Icon programming language quick reference Page 6

&phi &pi &pos &program &random &source &subject &time &trace &ucase &version

Golden ratio 0, 1.61803X X X % , 3.14159X X X Current scan position File name of executing program Pseudorandom seed Co-expression for the activator of the current coexpression Current scan string Milliseconds of cpu time since program start Controls procedure trace: 0 for none, -1 for I AX X X Z Current Icon version

Written by John W. Shipman (tcc-doc@nmt.edu). This version printed 2003-06-30. Copyright c 2001 by the New Mexico Institute of Mining and Technology.

New Mexico Tech Computer Center

Icon programming language quick reference

Page 7