home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 1: Collection A / 17Bit_Collection_A.iso / files / 134.dms / 134.adf / Icon / overview.doc < prev    next >
Text File  |  1993-02-16  |  10KB  |  374 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.                    An Overview of the Icon Programming
  15.                                 Language*
  16.  
  17.  
  18.                             Ralph E. Griswold
  19.  
  20.  
  21.  
  22.  
  23.                                 TR 83-3c
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.                  May 13, 1983; last revised May 1, 1986
  50.  
  51.  
  52.                      Department of Computer Science
  53.  
  54.                         The University of Arizona
  55.  
  56.                           Tucson, Arizona 85721
  57.  
  58.  
  59.  
  60.  
  61.     *This work was supported by the National Science Foundation under
  62.     Grant DCR-8401831.
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.                    An Overview of the Icon Programming
  77.                                 Language
  78.  
  79.  
  80.  
  81.  
  82.     _1_.___I_n_t_r_o_d_u_c_t_i_o_n
  83.  
  84.        Icon is a high-level programming language with extensive
  85.     facilities for processing strings and lists. Icon has several
  86.     novel features, including expressions that may produce sequences
  87.     of results, goal-directed evaluation that automatically searches
  88.     for a successful result, and string scanning that allows opera-
  89.     tions on strings to be formulated at a high conceptual level.
  90.  
  91.        Icon resembles SNOBOL4 [1] in its emphasis on high-level
  92.     string processing and a design philosophy that allows ease of
  93.     programming and short, concise programs. Like SNOBOL4, storage
  94.     allocation and garbage collection are automatic in Icon, and
  95.     there are few restrictions on the sizes of objects. Strings,
  96.     lists, and other structures are created during program execution
  97.     and their size does not need to be known when a program is writ-
  98.     ten.  Values are converted to expected types automatically; for
  99.     example, numeral strings read in as input can be used in numeri-
  100.     cal computations without explicit conversion.  Whereas SNOBOL4
  101.     has a pattern-matching facility that is separate from the rest of
  102.     the language, string scanning is integrated with the rest of the
  103.     language facilities in Icon.  Unlike SNOBOL4, Icon has an
  104.     expression-based syntax with reserved words; in appearance, Icon
  105.     programs resemble those of several other conventional programming
  106.     languages.
  107.  
  108.        Examples of the kinds of problems for which Icon is well
  109.     suited are:
  110.  
  111.          o+  text analysis, editing, and formatting
  112.  
  113.          o+  document preparation
  114.  
  115.          o+  symbolic mathematics
  116.  
  117.          o+  text generation
  118.  
  119.          o+  parsing and translation
  120.  
  121.          o+  data laundry
  122.  
  123.          o+  graph manipulation
  124.  
  125.        Version 6 of Icon, the most recent version, is implemented in
  126.     C [2]. There are UNIX* implementations for the Amdahl 580, the
  127.     __________________________
  128.     *UNIX is a trademark of AT&T Bell Laboratories.
  129.  
  130.                                   - 1 -
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.     AT&T 3B series, the HP 9000, the IBM PC/XT/AT, the PDP-11, the
  140.     Ridge 32, the Sun Workstation, and the VAX-11.  There also is a
  141.     VMS implementation for the VAX-11 and a DOS implementation for
  142.     personal computers. Other implementations are in progress.
  143.  
  144.        A brief description of some of the representative features of
  145.     Icon is given in the following sections. This description is not
  146.     rigorous and does not include many features of Icon. See [3] for
  147.     a complete description and [4] for a description of recent
  148.     changes to the language.
  149.  
  150.  
  151.     _2_.___S_t_r_i_n_g_s
  152.  
  153.        Strings of characters may be arbitrarily long, limited only by
  154.     the architecture of the computer on which Icon is implemented. A
  155.     string may be specified literally by enclosing it in double quo-
  156.     tation marks, as in
  157.  
  158.             greeting := "Hello world"
  159.  
  160.     which assigns an 11-character string to greeting, and
  161.  
  162.             address := ""
  163.  
  164.     which assigns the zero-length _e_m_p_t_y string to address.  The
  165.     number of characters in a string s, its size, is given by *s. For
  166.     example, *greeting is 11 and *address is 0.
  167.  
  168.        Icon uses the ASCII character set, extended to 256 characters.
  169.     There are escape conventions, similar to those of C, for
  170.     representing characters that cannot be keyboarded.
  171.  
  172.        Strings also can be read in and written out, as in
  173.  
  174.             line := read()
  175.  
  176.     and
  177.  
  178.             write(line)
  179.  
  180.     Strings can be constructed by concatenation, as in
  181.  
  182.             element := "(" || read() || ")"
  183.  
  184.     If the concatenation of a number of strings is to be written out,
  185.     the write function can be used with several arguments to avoid
  186.     actual concatenation:
  187.  
  188.             write("(",read(),")")
  189.  
  190.  
  191.        Substrings can be formed by subscripting strings with range
  192.     specifications that indicate, by position, the desired range of
  193.  
  194.  
  195.  
  196.                                   - 2 -
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.     characters. For example,
  206.  
  207.             middle := line[10:20]
  208.  
  209.     assigns to middle the string of characters of line between posi-
  210.     tions 10 and 20.  Similarly,
  211.  
  212.             write(line[2])
  213.  
  214.     writes the second character of line.  The value 0 is used to
  215.     refer to the position after the last character of a string. Thus
  216.  
  217.             write(line[2:0])
  218.  
  219.     writes the substring of line from the second character to the
  220.     end, thus omitting the first character.
  221.  
  222.        An assignment can be made to the substring of string-valued
  223.     variable to change its value. For example,
  224.  
  225.             line[2] := "..."
  226.  
  227.     replaces the second character of line by three dots. Note that
  228.     the size of line changes automatically.
  229.  
  230.        There are many functions for analyzing strings. An example is
  231.  
  232.             find(s1,s2)
  233.  
  234.     which produces the position in s2 at which s1 occurs as a sub-
  235.     string. For example, if the value of greeting is as given ear-
  236.     lier,
  237.  
  238.             find("or",greeting)
  239.  
  240.     produces the value 8.  See Section 4.2 for the handling of situa-
  241.     tions in which s1 does not occur in s2, or in which it occurs at
  242.     several different positions.
  243.  
  244.  
  245.     _3_.___C_h_a_r_a_c_t_e_r__S_e_t_s
  246.  
  247.        While strings are sequences of characters, _c_s_e_t_s are sets of
  248.     characters in which membership rather than order is significant.
  249.     Csets are represented literally using single enclosing quotation
  250.     marks, as in
  251.  
  252.             vowels := 'aeiouAEIOU'
  253.  
  254.     Two useful built-in csets are &lcase and &ucase, which consist of
  255.     the lowercase and uppercase letters, respectively.  Set opera-
  256.     tions are provided for csets. For example,
  257.  
  258.  
  259.  
  260.  
  261.  
  262.                                   - 3 -
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.             letters := &lcase ++ &ucase
  272.  
  273.     forms the cset union of the lowercase and uppercase letters and
  274.     assigns the resulting cset to letters, while
  275.  
  276.             consonants := letters -- 'aeiouAEIOU'
  277.  
  278.     forms the cset difference of the letters and the vowels and
  279.     assigns the resulting cset to consonants.
  280.  
  281.        Csets are useful in situations in which any one of a number of
  282.     characters is significant. An example is the string analysis
  283.     function
  284.  
  285.             upto(c,s)
  286.  
  287.     which produces the position s at which any character in c occurs.
  288.     For example,
  289.  
  290.             upto(vowels,greeting)
  291.  
  292.     produces 2. Another string analysis function that uses csets is
  293.  
  294.             many(c,s)
  295.  
  296.     which produces the position in s after an initial substring con-
  297.     sisting only of characters that occur in s.  An example of the
  298.     use of many is in locating words. Suppose, for example, that a
  299.     word is defined to consist of a string of letters.  The expres-
  300.     sion
  301.  
  302.             write(line[1:many(letters,line)])
  303.  
  304.     writes a word at the beginning of line. Note the use of the posi-
  305.     tion returned by a string analysis function to specify the end of
  306.     a substring.
  307.  
  308.  
  309.     _4_.___E_x_p_r_e_s_s_i_o_n__E_v_a_l_u_a_t_i_o_n
  310.  
  311.     _4_._1___C_o_n_d_i_t_i_o_n_a_l__E_x_p_r_e_s_s_i_o_n_s
  312.  
  313.        In Icon there are _c_o_n_d_i_t_i_o_n_a_l _e_x_p_r_e_s_s_i_o_n_s that may _s_u_c_c_e_e_d and
  314.     produce a result, or may _f_a_i_l and not produce any result. An
  315.     example is the comparison operation
  316.  
  317.             i > j
  318.  
  319.     which succeeds (and produces the value of j) provided that the
  320.     value of i is greater than the value of j, but fails otherwise.
  321.  
  322.        The success or failure of conditional operations is used
  323.     instead of Boolean values to drive control structures in Icon. An
  324.     example is
  325.  
  326.  
  327.  
  328.                                   - 4 -
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.             if i > j then k := i else k := j
  338.  
  339.     which assigns the value of i to k if the value of i is greater
  340.     than the value of j, but assigns the value of j to k otherwise.
  341.  
  342.        The usefulness of the concepts of success and failure is
  343.     illustrated by find(s1,s2), which fails if s1 does not occur as a
  344.     substring of s2.  Thus
  345.  
  346.             if i := find("or",line) then write(i)
  347.  
  348.     writes the position at which or occurs in line, if it occurs, but
  349.     does not write a value if it does not occur.
  350.  
  351.        Many expressions in Icon are conditional. An example is
  352.     read(), which produces the next line from the input file, but
  353.     fails when the end of the file is reached. The following expres-
  354.     sion is typical of programming in Icon and illustrates the
  355.     integration of conditional expressions and conventional control
  356.     structures:
  357.  
  358.             while line := read() do
  359.                write(line)
  360.  
  361.     This expression copies the input file to the output file.
  362.  
  363.        If an argument of a function fails, the function is not
  364.     called, and the function call fails as well. This ``inheritance''
  365.     of failure allows the concise formulation of many programming
  366.     tasks. Omitting the optional do clause in while-do, the previous
  367.     expression can be rewritten as
  368.  
  369.             while write(read())
  370.  
  371.  
  372.     _4_._2___G_e_n_e_r_a_t_o_r_s
  373.  
  374.        In some sit