Previous Table of Contents Next


6.2.2.5. Conjunction

Sometimes it is useful to bind several expressions together for goal-directed evaluation. As mentioned earlier, if the expressions are separated by semicolons or, equivalently, written on separate lines, goal-directed evaluation goes from one to the next. For example, in

    expr1; expr2

after the evaluation of expr1 is complete, it no longer is involved in goal-directed evaluation. In particular, if expr2 fails, there is no resumption of generators in expr1. They have, in fact, been discarded.

To prevent this, the expressions can be combined using conjunction:

    expr1 & expr2

In conjunction, failure of expr2 causes the resumption of suspended generators in expr1. The net effect of this is that

    expr1 & expr2

succeeds only if both expr1 and expr2 succeed. This sometimes is referred to as “and” in contrast with the “or” of alternation. For example,

    if (i = 0) & (j = 0) then proceed()

evaluates the then clause only if both i and j are equal to 0.

As with alternation, many expressions can be combined for goal-directed evaluation using conjunction:

    expr1 & expr2 & expr3 & … & exprn

If a conjunction succeeds, its value is the value of the last expression in it. In the preceding example, it is the value of exprn.

6.2.2.6. Backtracking

The process of resuming suspended generators, which returns control to sites of earlier evaluation, is called control backtracking. Control backtracking is implicit in expression evaluation in Icon and occurs automatically.

Data backtracking is the process of restoring previous values during control backtracking to undo the effects of previous assignments. Icon ordinarily does not perform data backtracking. For example, in
    (count := 0) & expr

if expr fails, the value of count remains 0 regardless of its value before the assignment.

Icon provides a form of assignment that is reversible. This operation is <–. For example, in

    (count <– 0) & expr

if expr fails, the value of count is restored to what it was prior to the reversible assignment.

6.2.3. String Processing

Icon has powerful facilities for manipulating strings of characters. It does not, however, have a character data type. Instead, it has two data types that are composed of characters:

  csets, which are unordered collections of distinct characters
  strings, which are ordered sequences of characters

All 256 characters in the 8-bit extended ASCII character set can be used in csets and strings.

6.2.3.1. Csets

Icon has several built-in csets, including:

&digits—The 10 decimal digits
&ucase—The 26 uppercase letters
&lcase—The 26 lowercase letters
&letters—The 52 upper- and lowercase letters
&cset—All 256 characters

Csets can be specified literally by enclosing characters in single quotation marks, as in

    vowels := ‘AEIOUaeiou’

The empty cset, containing no characters, is specified literally by two single quotation marks: ‘ ’.

Csets can be constructed by set operations on other csets:

c1 ++ c2, the union of c1 and c2; all the characters in either of them
c1 ** c2, the intersection of c1 and c2; all the characters in both of them
c1 –– c2, the difference between c1 and c2; all the characters c1 not in c2
~c, the complement of c with respect to &cset; all the characters not in c

6.2.3.2. Strings

Strings can be arbitrarily long. They can be specified literally by enclosing a sequence of characters in double quotation marks, as in

    place := “Mars”

The empty string, containing no characters, is specified by two double quotation marks: “ “.

The operation *s produces the length of s—the number of characters in it. For example, *place produces 4.

String Construction

The most basic operation for constructing strings is concatenation, one string followed by another. Concatenation is specified by s1 || s2, as in

    salutation := “Hi there”
    greeting := salutation || “ “ || place || “.”

which assigns the string “Hi there Mars.” to greeting.

Multiple copies of a string can be concatenated at one time using the function repl(s, i), which produces i copies of s, as in

    banner := repl(“+–”, 20)

which assigns to banner a 40-character string of alternating +s and s.

There are three functions for producing strings of a fixed length:

left(s1, i, s2)—Produces a string of length i with s1 starting at the left and copies of s2 used for padding at the right, if necessary
right(s1, i, s2)—Produces a string of length i with s1 ending at the right and copies of s2 used for padding at the left, if necessary
center(s1, i, s2)—Produces a string of length i with s1 centered and copies of s2 used for padding at the left and right, if necessary

For example, right(“61”, 4, “0”) produces “0061”.

If *s1 is less than i, it is truncated at the right, left, and ends for the three functions, respectively. If s2 is omitted, it defaults to a blank.

The function trim(s, c) returns the result of trimming all the characters in c off the right end of s. If c is omitted, blanks are trimmed.


Previous Table of Contents Next