home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / unix_c / languags / icon / library6.doc < prev    next >
Encoding:
Text File  |  1988-11-28  |  52.0 KB  |  2,113 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.                      The Icon Program Library
  15.                       Version 6; Release 1*
  16.  
  17.  
  18.                         Ralph E. Griswold
  19.  
  20.  
  21.  
  22.  
  23.                             TR 86-13b
  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.           June 12, 1986, Last revised October 17, 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.                      The Icon Program Library
  77.                       Version 6; Release 1
  78.  
  79.  
  80.  
  81. Introduction
  82.  
  83.    This version of the Icon program library is intended for use
  84. with Version 6 of Icon.  Basic documentation for Version 6 of
  85. Icon is contained in the Icon book [1] and a supplementary report
  86. [2].
  87.  
  88.    The library contains both complete programs and collections of
  89. procedures. The programs range from demonstrations and games to
  90. text-processing utilities. The procedures range from straightfor-
  91. ward extensions to Icon's function repertoire to such relatively
  92. esoteric subjects as programmer-defined control operations.  This
  93. manual is divided into two main parts according to the composi-
  94. tion of the library: complete programs and collections of pro-
  95. cedures.
  96.  
  97.    While the library provides some useful application programs
  98. and components that may be helpful in building other programs, it
  99. also provides examples of Icon programming techniques. In partic-
  100. ular, persons who are new to Icon may find it helpful to read the
  101. source code for the library to see how experienced persons pro-
  102. gram in Icon. While not all of the code is the best possible -
  103. far from it - it illustrates useful idioms and a variety program-
  104. ming techniques.
  105.  
  106.    In the descriptions that follow, there are pointers to
  107. interesting programming techniques as well as several suggestions
  108. for extensions and improvements to programs.  Such extensions are
  109. good exercises persons who are just starting in Icon. Some of
  110. these extensions, however, will challenge the most experienced
  111. Icon programmer.
  112.  
  113. Library Format
  114.  
  115.    The root directory of the library is ipl (``Icon program
  116. library'').  There are four subdirectories: source, progs, procs,
  117. and data.  The subdirectory source contains Icon source code for
  118. both programs and procedure libraries. Compiled programs are in
  119. progs and translated procedures are in procs.  The subdirectory
  120. data contains sample input for programs in progs. The names of
  121. programs and data files generally coincide, with the extensions
  122. of data files providing some differentiating identification. For
  123. example, the data file csgen.abc is input to the program csgen.
  124. There are also several files with the extension .txt that contain
  125. English-language text that is suitable as input to any of the
  126. programs that process text files.
  127.  
  128.  
  129.  
  130.                               - 1 -
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. Disclaimer
  140.  
  141.    The material contained in the Icon program library is provided
  142. on an as-is basis. No claim is made that the programs are free of
  143. error or that they will function properly.  The responsibility
  144. for the use of library material resides entirely with the user.
  145.  
  146.    Notes of errors will be appreciated and corrections will be
  147. incorporated in future releases of the library.
  148.  
  149. New Material
  150.  
  151.    Additions are made to the Icon program library from time to
  152. time.  New material is welcome. Such material should be sent to:
  153.  
  154.         Icon Project
  155.         Department of Computer Science
  156.         The University of Arizona
  157.         Tucson, AZ   87521
  158.  
  159. Documentation similar in form to that provided in this manual
  160. must be included and test data should be provided where appropri-
  161. ate.  The final decision on inclusion of material in the library
  162. resides with the Icon Project.
  163.  
  164. Acknowledgements
  165.  
  166.    Several persons have contributed programs and procedures to
  167. the Icon program library.  In addition to the author of this
  168. manual, these persons include Allan Anderson, Ward Cunningham,
  169. Tom Hicks, William Malloy, Bill Mitchell, Mike Novak, Randal
  170. Schwartz, Steve Wampler, and George Yee. See the source files for
  171. specific attributions.
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.                               - 2 -
  197.  
  198.  
  199.  
  200.  
  201.  
  202.                             Programs
  203.  
  204.  
  205. Introduction
  206.  
  207.    Most programs take input from standard input and write output
  208. to standard output.  Input and output can be redirected and piped
  209. in the usual fashion.  For example,
  210.  
  211.         csgen <../data/csgen.abc | more
  212.  
  213. runs the program csgen on the data file csgen.abc in the parallel
  214. subdirectory and pipes the output through more1.
  215.  
  216.    Many programs take command line arguments, which may be the
  217. names of files to process or options that select specific pro-
  218. cessing functions.  An option is prefixed by a dash, sometimes
  219. followed by an argument. For example,
  220.  
  221.         deal -h 5
  222.  
  223. runs the program deal with the option -h and the argument 5.
  224.  
  225.    If a program is not called with the proper options or argu-
  226. ments, it generally terminates with an error message such as
  227.  
  228.         usage: [ -h n ] [ -s n ]
  229.  
  230. which indicates the proper usage. Some programs provide more
  231. specific errors messages. Error messages are written to standard
  232. error output. Standard error output is always written to the con-
  233. sole and cannot be redirected.  Consult the descriptions of the
  234. programs that follow for details.
  235.  
  236.    The programs that follow are divided into categories by their
  237. function.
  238.  
  239.  
  240. 1.  Demonstrations and Games
  241.  
  242.  
  243. 1.1  Non-Attacking Queens: queens
  244.  
  245.    This program displays the solutions to the non-attacking n-
  246. queens problem: the ways in which n queens can be placed on an
  247. n-by-n chessboard so that no queen can attack another. A positive
  248. integer can be given as a command line argument to specify the
  249. number of queens. For example,
  250.                           
  251. 1Path syntax is system dependent. On some systems, including
  252. MS-DOS  [3]  and  VMS  [4],  compiled programs cannot be run
  253. directly, but must be executed using iconx. For MS-DOS,  the
  254. example above is done as follows:
  255.  
  256.         iconx csgen <..\data\csgen.abc | more
  257.  
  258.  
  259.  
  260.  
  261.  
  262.                               - 3 -
  263.  
  264.  
  265.  
  266.  
  267.  
  268.                             Programs
  269.  
  270.  
  271.         iconx queens 8
  272.  
  273. displays the solutions for 8 queens on an 8-by-8 chessboard.  The
  274. default value in the absence of an argument is 6.  One solution
  275. for six queens is:
  276.        -------------------------
  277.        |   | Q |   |   |   |   |
  278.        -------------------------
  279.        |   |   |   | Q |   |   |
  280.        -------------------------
  281.        |   |   |   |   |   | Q |
  282.        -------------------------
  283.        | Q |   |   |   |   |   |
  284.        -------------------------
  285.        |   |   | Q |   |   |   |
  286.        -------------------------
  287.        |   |   |   |   | Q |   |
  288.        -------------------------
  289.  
  290. Comments: There are many approaches to programming solutions to
  291. the n-queens problem.  This library program is worth reading for
  292. its programming techniques. Other solutions may be found in [1]
  293. and [5].
  294.  
  295. 1.2  Word Intersections: cross
  296.  
  297.    This program takes a list of words and tries to arrange them
  298. in cross-word format so that they intersect. Uppercase letters
  299. are mapped into lowercase letters on input.  For example, the
  300. input
  301.  
  302.         and
  303.         eggplants
  304.         elephants
  305.         purple
  306.  
  307. produces the output
  308.      +---------+
  309.      | p       |
  310.      | u e     |
  311.      | r g     |
  312.      | p g     |
  313.      |elephants|
  314.      | e l     |
  315.      |   and   |
  316.      |   n     |
  317.      |   t     |
  318.      |   s     |
  319.      +---------+
  320.  
  321. Diagnostics: The program objects if the input contains a nonal-
  322. phabetic character.
  323.  
  324. Comments: This program produces only one possible intersection
  325.  
  326.  
  327.  
  328.                               - 4 -
  329.  
  330.  
  331.  
  332.  
  333.  
  334.                             Programs
  335.  
  336.  
  337. and it does not attempt to produce the most compact result.  The
  338. program is not very fast, either.  There is a lot of room for
  339. improvement here. In particular, it is natural for Icon to gen-
  340. erate a sequence of solutions.
  341.  
  342. 1.3  Bridge Hands: deal
  343.  
  344.    This program shuffles, deals, and displays hands in the game
  345. of bridge.  An example of the output of deal is
  346.      ---------------------------------
  347.  
  348.                S: KQ987
  349.                H: 52
  350.                D: T94
  351.                C: T82
  352.  
  353.      S: 3                S: JT4
  354.      H: T7               H: J9863
  355.      D: AKQ762           D: J85
  356.      C: QJ94             C: K7
  357.  
  358.                S: A652
  359.                H: AKQ4
  360.                D: 3
  361.                C: A653
  362.  
  363.      ---------------------------------
  364.  
  365. Options: The following options are available:
  366.  
  367.      -h n Produce n hands. The default is 1.
  368.  
  369.      -s n Set the seed for random generation to n.  Different
  370.           seeds give different hands.  The default seed is 0.
  371.  
  372. 1.4  Farberisms: farb
  373.  
  374.    Dave Farber, co-author of the original SNOBOL programming
  375. language, is noted for his creative use of the English language.
  376. Hence the terms ``farberisms'' and ``to farberate''.  This pro-
  377. gram produces a randomly selected farberism.
  378.  
  379. Notes: Not all of the farberisms contained in this program were
  380. uttered by the master himself; others have learned to emulate
  381. him.  A few of the farberisms may be objectionable to some per-
  382. sons.  ``I wouldn't marry her with a twenty-foot pole.''
  383.  
  384.  
  385. 2.  Random Strings
  386.  
  387.    The programs in this section involve the random generation of
  388. strings according to various criteria.  These programs are only
  389. loosely related to each other.
  390.  
  391.  
  392.  
  393.  
  394.                               - 5 -
  395.  
  396.  
  397.  
  398.  
  399.  
  400.                             Programs
  401.  
  402.  
  403. 2.1  Random Sentence Generation: rsg
  404.  
  405.    This program generates randomly selected strings (``sen-
  406. tences'') from a grammar specified by the user.  Grammars are
  407. basically context-free and resemble BNF in form, although there
  408. are a number of extensions.
  409.  
  410.    The program works interactively, allowing the user to build,
  411. test, modify, and save grammars. Input to rsg consists of various
  412. kinds of specifications, which can be intermixed:
  413.  
  414.    Productions define nonterminal symbols in a syntax similar to
  415. the rewriting rules of BNF with various alternatives consisting
  416. of the concatenation of nonterminal and terminal symbols.  Gen-
  417. eration specifications cause the generation of a specified number
  418. of sentences from the language defined by a given nonterminal
  419. symbol.  Grammar output specifications cause the definition of a
  420. specified nonterminal or the entire current grammar to be written
  421. to a given file.  Source specifications cause subsequent input to
  422. be read from a specified file.
  423.  
  424.    In addition, any line beginning with # is considered to be a
  425. comment, while any line beginning with = causes the rest of that
  426. line to be used subsequently as a prompt to the user whenever rsg
  427. is ready for input (there normally is no prompt). A line consist-
  428. ing of a single = stops prompting.
  429.  
  430. Productions: Examples of productions are:
  431.  
  432.         <expr>::=<term>|<term>+<expr>
  433.         <term>::=<elem>|<elem>*<term>
  434.         <elem>::=x|y|z|(<expr>)
  435.  
  436. Productions may occur in any order. The definition for a nonter-
  437. minal symbol can be changed by specifying a new production for
  438. it.
  439.  
  440.    There are a number of special devices to facilitate the defin-
  441. ition of grammars, including eight predefined, built-in nontermi-
  442. nal symbols:
  443.    symbol   definition
  444.    <lb>     <
  445.    <rb>     >
  446.    <vb>     |
  447.    <nl>     newline
  448.    <>       empty string
  449.    <&lcase> any single lowercase letter
  450.    <&ucase> any single uppercase letter
  451.    <&digit> any single digit
  452.  
  453. In addition, if the string between a < and a > begins and ends
  454. with a single quotation mark, it stands for any single character
  455. between the quotation marks. For example,
  456.  
  457.  
  458.  
  459.  
  460.                               - 6 -
  461.  
  462.  
  463.  
  464.  
  465.  
  466.                             Programs
  467.  
  468.  
  469.         <'xyz'>
  470.  
  471. is equivalent to
  472.  
  473.         x|y|z
  474.  
  475. Finally, if the name of a nonterminal symbol between the < and >
  476. begins with ?, the user is queried during generation to supply a
  477. string for that nonterminal symbol. For example, in
  478.  
  479.         <expr>::=<?term>|<term>+<expr>
  480.  
  481. if the first alternative is encountered during generation, the
  482. user is asked to provide a string for <term>.  Note that this is
  483. a strongly context-sensitive feature.
  484.  
  485. Generation Specifications: A generation specification consists of
  486. a nonterminal symbol followed by a nonnegative integer. An exam-
  487. ple is
  488.  
  489.         <expr>10
  490.  
  491. which specifies the generation of 10 <expr>s. If the integer is
  492. omitted, it is assumed to be 1. Generated sentences are written
  493. to standard output.
  494.  
  495. Grammar Output Specifications: A grammar output specification
  496. consists of a nonterminal symbol, followed by ->, followed by a
  497. file name. Such a specification causes the current definition of
  498. the nonterminal symbol to be written to the given file. If the
  499. file is omitted, standard output is assumed. If the nonterminal
  500. symbol is omitted, the entire grammar is written out. Thus,
  501.  
  502.         ->
  503.  
  504. causes the entire grammar to be written to standard output.
  505.  
  506. Source Specifications: A source specification consists of @ fol-
  507. lowed by a file name.  Subsequent input is read from that file.
  508. When an end of file is encountered, input reverts to the previous
  509. file. Input files can be nested.
  510.  
  511. Options: The following options are available:
  512.  
  513.      -s n Set the seed for random generation to n.  The default
  514.           seed is 0.
  515.  
  516.      -l n Terminate generation if the number of symbols remaining
  517.           to be processed exceeds n. There is no default limit.
  518.  
  519.      -t   Trace the generation of sentences. Trace output goes to
  520.           standard error output.
  521.  
  522. Diagnostics: Syntactically erroneous input lines are noted but
  523.  
  524.  
  525.  
  526.                               - 7 -
  527.  
  528.  
  529.  
  530.  
  531.  
  532.                             Programs
  533.  
  534.  
  535. are otherwise ignored.  Specifications for a file that cannot be
  536. opened are noted and treated as erroneous.
  537.  
  538.    If an undefined nonterminal symbol is encountered during gen-
  539. eration, an error message that identifies the undefined symbol is
  540. produced, followed by the partial sentence generated to that
  541. point. Exceeding the limit of symbols remaining to be generated
  542. as specified by the -l option is handled similarly.
  543.  
  544. Caveats: Generation may fail to terminate because of a loop in
  545. the rewriting rules or, more seriously, because of the progres-
  546. sive accumulation of nonterminal symbols. The latter problem can
  547. be identified by using the -t option and controlled by using the
  548. -l option. The problem often can be circumvented by duplicating
  549. alternatives that lead to fewer rather than more nonterminal sym-
  550. bols. For example, changing
  551.  
  552.         <term>::=<elem>|<elem>*<term>
  553.  
  554. to
  555.  
  556.         <term>::=<elem>|<elem>|<elem>*<term>
  557.  
  558. increases the probability of selecting <elem> from 1/2 to 2/3.
  559. See [6] for a discussion of the general problem.
  560.  
  561. Comments: This program is an extension and elaboration of a pro-
  562. gram described in some detail in [1]. It illustrates many
  563. features of Icon, including a combination of string and list pro-
  564. cessing as well as extensive use of generators. The source code
  565. is worth studying.
  566.  
  567.    There are many possible extensions to the program. One of the
  568. most useful would be a way to specify the probability of select-
  569. ing an alternative.
  570.  
  571. 2.2  Context-Sensitive Generation: csgen
  572.  
  573.    This program accepts a context-sensitive production grammar
  574. and generates randomly selected sentences from the corresponding
  575. language.  See [7] for a discussion of such grammars.
  576.  
  577.    Uppercase letters stand for nonterminal symbols and -> indi-
  578. cates the lefthand side can be rewritten by the righthand side.
  579. Other characters are considered to be terminal symbols. Lines
  580. beginning with # are considered to be comments and are ignored.
  581. A line consisting of a nonterminal symbol followed by a colon and
  582. a nonnegative integer i is a generation specification for i
  583. instances of sentences for the language defined by the nontermi-
  584. nal (goal) symbol.  An example of input to csgen is:
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.                               - 8 -
  593.  
  594.  
  595.  
  596.  
  597.  
  598.                             Programs
  599.  
  600.  
  601.         #   a(n)b(n)c(n)
  602.         #   Salomaa, p. 11.
  603.         #   Attributed to M. Soittola.
  604.         #
  605.         X->abc
  606.         X->aYbc
  607.         Yb->bY
  608.         Yc->Zbcc
  609.         bZ->Zb
  610.         aZ->aaY
  611.         aZ->aa
  612.         X:10
  613.  
  614. The output of csgen for this example is
  615.  
  616.         aaabbbccc
  617.         aaaaaaaaabbbbbbbbbccccccccc
  618.         abc
  619.         aabbcc
  620.         aabbcc
  621.         aaabbbccc
  622.         aabbcc
  623.         abc
  624.         aaaabbbbcccc
  625.         aaabbbccc
  626.  
  627.  
  628.    A positive integer followed by a colon can be prefixed to a
  629. production to replicate that production, making its selection
  630. more likely. For example,
  631.  
  632.         3:X->abc
  633.  
  634. is equivalent to
  635.  
  636.         X->abc
  637.         X->abc
  638.         X->abc
  639.  
  640.  
  641. Option: The -t option writes a trace of the derivations to stan-
  642. dard error output.
  643.  
  644. Limitations: Nonterminal symbols can only be represented by sin-
  645. gle uppercase letters, and there is no way to represent uppercase
  646. letters as terminal symbols.
  647.  
  648.    There can be only one generation specification and it must
  649. appear as the last line of input.
  650.  
  651. Comments: Generation of context-sensitive strings is a slow pro-
  652. cess. It may not terminate, either because of a loop in the
  653. rewriting rules or because of the progressive accumulation of
  654. nonterminal symbols.  The program avoids deadlock, in which there
  655.  
  656.  
  657.  
  658.                               - 9 -
  659.  
  660.  
  661.  
  662.  
  663.  
  664.                             Programs
  665.  
  666.  
  667. are no possible rewrites for a string in the derivation.
  668.  
  669.    This program would be improved if the specification of nonter-
  670. minal symbols were more general, as in rsg.
  671.  
  672. 2.3  Parenthesis-Balanced Strings: parens
  673.  
  674.    This program produces parenthesis-balanced strings in which
  675. the parentheses are randomly distributed.
  676.  
  677. Options: The following options are available:
  678.  
  679.      -b n Bound the length of the strings to n left and right
  680.           parentheses each. The default is 10.
  681.  
  682.      -n n Produce n strings. The default is 10.
  683.  
  684.      -l s Use the string s for the left parenthesis. The default
  685.           is ( .
  686.  
  687.      -r s Use the string s for the right parenthesis. The default
  688.           is ) .
  689.  
  690.      -v   Randomly vary the length of the strings between 0 and
  691.           the bound.  In the absence of this option, all strings
  692.           are the exactly as long as the specified bound.
  693.  
  694.    For example, the output for
  695.  
  696.         parens -v -b 4 -l "begin " -r "end "
  697.  
  698. is
  699.  
  700.         begin end
  701.         begin end begin end
  702.         begin begin end end begin end
  703.         begin end begin begin end end
  704.         begin end
  705.         begin begin end end
  706.         begin begin begin end end end
  707.         begin end begin begin end end
  708.         begin end begin end
  709.         begin begin end begin end begin end end
  710.  
  711.  
  712. Comments: This program was motivated by the need for test data
  713. for error repair schemes for block-structured programming lan-
  714. gauges. See [8]. A useful extension to this program would be some
  715. way of generating other text among the parentheses.  In addition
  716. to the intended use of the program, it can produce a variety of
  717. interesting patterns, depending on the strings specified by -l
  718. and -r.
  719.  
  720.  
  721.  
  722.  
  723.  
  724.                              - 10 -
  725.  
  726.  
  727.  
  728.  
  729.  
  730.                             Programs
  731.  
  732.  
  733. 2.4  Shuffled Files: shuffile
  734.  
  735.    This program writes a version of the input file with the lines
  736. shuffled.  For example, the result of shuffling
  737.  
  738.                  On the Future!-how it tells
  739.                  Of the rapture that impells
  740.                 To the swinging and the ringing
  741.                  Of the bells, bells, bells-
  742.               Of the bells, bells, bells, bells,
  743.                         Bells, bells, bells-
  744.           To the rhyming and the chiming of the bells!
  745.  
  746. is
  747.  
  748.           To the rhyming and the chiming of the bells!
  749.                 To the swinging and the ringing
  750.                         Bells, bells, bells-
  751.                  Of the bells, bells, bells-
  752.                  On the Future!-how it tells
  753.               Of the bells, bells, bells, bells,
  754.                  Of the rapture that impells
  755.  
  756.  
  757. Option: The option -s n sets the seed for random generation to n.
  758. The default seed is 0.
  759.  
  760. Limitation: This program stores the input file in memory and
  761. shuffles pointers to the lines; there must be enough memory
  762. available to store the entire file.
  763.  
  764.  
  765. 3.  Text Tabulation
  766.  
  767. 3.1  Character Tabulation: tablc
  768.  
  769.    This program tabulates characters and lists each character and
  770. the number of times it occurs. Characters are written using
  771. Icon's escape conventions.  Line termination characters and other
  772. control characters are included in the tabulation.
  773.  
  774. Options: The following options are available:
  775.  
  776.      -a   Write the summary in alphabetical order of the charac-
  777.           ters. This is the default.
  778.  
  779.      -n   Write the summary in numerical order of the counts.
  780.  
  781.      -u   Write only the characters that occur just once.
  782.  
  783. 3.2  Word Tabulation: tablw
  784.  
  785.    This program tabulates words and lists number of times each
  786. word occurs. A word is defined to be a string of consecutive
  787.  
  788.  
  789.  
  790.                              - 11 -
  791.  
  792.  
  793.  
  794.  
  795.  
  796.                             Programs
  797.  
  798.  
  799. upper- and lowercase letters with at most one interior occurrence
  800. of a dash or apostrophe.
  801.  
  802. Options: The following options are available:
  803.  
  804.      -a   Write the summary in alphabetical order of the words.
  805.           This is the default.
  806.  
  807.      -i   Ignore case distinctions among letters; uppercase
  808.           letters are mapped into to corresponding lowercase
  809.           letters on input. The default is to maintain case dis-
  810.           tinctions.
  811.  
  812.      -n   Write the summary in numerical order of the counts.
  813.  
  814.      -l n Tabulate only words longer than n characters. The
  815.           default is to tabulate all words.
  816.  
  817.      -u   Write only the words that occur just once.
  818.  
  819.  
  820. 4.  Mailing Labels
  821.  
  822. 4.1  Produce Mailing Labels: labels
  823.  
  824.    This program produces labels using coded information taken
  825. from the input file.  In the input file, a line beginning with #
  826. is a label header.  Subsequent lines up to the next header or
  827. end-of-file are accumulated and output so as to be centered hor-
  828. izontally and vertically on label forms.  Lines beginning with *
  829. are treated as comments and are ignored.
  830.  
  831. Options: The following options are available:
  832.  
  833.      -c n Print n copies of each label.
  834.  
  835.      -s s Select only those labels whose headers contain a char-
  836.           acter in s.
  837.  
  838.      -t   Format for curved tape labels (the default is to format
  839.           for rectangular mailing labels).
  840.  
  841.      -w n Limit line width to n characters. The default width is
  842.           40.
  843.  
  844.      -l n Limit the number of printed lines per label to n. The
  845.           default is 8.
  846.  
  847.      -d n Limit the depth of the label to n. The default is 9 for
  848.           rectangular labels and 12 for tape labels (-t).
  849.  
  850.      -f   Print the first line of each selected entry instead of
  851.           labels.
  852.  
  853.  
  854.  
  855.  
  856.                              - 12 -
  857.  
  858.  
  859.  
  860.  
  861.  
  862.                             Programs
  863.  
  864.  
  865.    Options are processed from left to right.  If the number of
  866. printed lines is set to a value that exceeds the depth of the
  867. label, the depth is set to the number of lines.  If the depth is
  868. set to a value that is less than the number of printed lines, the
  869. number of printed lines is set to the depth. Note that the order
  870. in which these options are specified may affect the results.
  871.  
  872. Printing Labels: Label forms should be used with a pin-feed pla-
  873. ten.  For mailing labels, the carriage should be adjusted so that
  874. the first character is printed at the leftmost position on the
  875. label and so that the first line of the output is printed on the
  876. topmost line of the label.  For curved tape labels, some experi-
  877. mentation may be required to get the text positioned properly.
  878.  
  879. Diagnostics: If the limits on line width or the number of lines
  880. per label are exceeded, a label with an error message is written
  881. to standard error output.
  882.  
  883. 4.2  Zip Code Sorting: zipsort
  884.  
  885.    This program sorts labels produced by labels in ascending
  886. order of their postal zip codes.
  887.  
  888. Option: The option -d n sets the number of lines per label to n.
  889. The default is 9. This value must agree with the value used to
  890. format the labels.
  891.  
  892. Zip Codes: The zip code must be the last nonblank string at the
  893. end of the label.  It must consist of digits but may have an
  894. embedded dash for extended zip codes.  If a label does not end
  895. with a legal zip code, it is placed after all labels with legal
  896. zip codes.  In such a case, an error messages also is written to
  897. standard error output.
  898.  
  899.  
  900. 5.  Laminated Files
  901.  
  902. 5.1  Laminating Files: lam
  903.  
  904.    This program laminates files named on the command line onto
  905. the standard output, producing a concatenation of corresponding
  906. lines from each file named.  If the files are different lengths,
  907. empty lines are substituted for missing lines in the shorter
  908. files.  A command line argument of the form - s causes the string
  909. s to be inserted between the concatenated file lines.
  910.  
  911.    Each command line argument is placed in the output line at the
  912. point that it appears in the argument list.  For example, lines
  913. from file1 and file2 can be laminated with a colon between each
  914. line from file1 and the corresponding line from file2 by the com-
  915. mand
  916.  
  917.         lam file1 -: file2
  918.  
  919.  
  920.  
  921.  
  922.                              - 13 -
  923.  
  924.  
  925.  
  926.  
  927.  
  928.                             Programs
  929.  
  930.  
  931.    File names and strings may appear in any order in the argument
  932. list.  If - is given for a file name, standard input is read at
  933. that point.  If a file is named more than once, each of its lines
  934. will be duplicated on the output line, except that if standard
  935. input is named more than once, its lines will be read alter-
  936. nately.  For example, each pair of lines from standard input can
  937. be joined onto one line with a space between them by the command
  938.  
  939.         lam - "- " -
  940.  
  941.  
  942. while the command
  943.  
  944.         lam file1 "- " file1
  945.  
  946.  
  947. replicates each line from file1.
  948.  
  949. 5.2  Delaminating Files: delam
  950.  
  951.    This program delaminates standard input into several output
  952. files according to the specified fields.  It writes the fields in
  953. each line to the corresponding output files as individual lines.
  954. If no data occurs in the specified position for a given input
  955. line an empty output line is written. This insures that all out-
  956. put files contain the same number of lines as the input file.
  957.  
  958.    If - is used for the input file, the standard input is read.
  959. If - is used as an output file name, the corresponding field is
  960. written to the standard output.
  961.  
  962.    The fields are defined by a list of field specifications,
  963. separated by commas, colons, or semicolons, of the following
  964. form:
  965.  
  966.         n    the character in column n
  967.         n-m  the characters in columns n through m
  968.         n+m  m characters beginning at column n
  969.  
  970. where the columns in a line are numbered from 1 to the length of
  971. the line.
  972.  
  973.    The use of delam is illustrated by the following examples.
  974. The command
  975.  
  976.         delam 1-10,5 x.txt y.txt
  977.  
  978. reads standard input and writes characters 1 through 10 to file
  979. x.txt and character 5 to file y.txt.  The command
  980.  
  981.         delam 10+5:1-10:1-10:80 mid x1 x2 end
  982.  
  983. writes characters 10 through 14 to mid, 1 through 10 to x1 and
  984. x2, and character 80 to end.  The command
  985.  
  986.  
  987.  
  988.                              - 14 -
  989.  
  990.  
  991.  
  992.  
  993.  
  994.                             Programs
  995.  
  996.  
  997.         delam 1-80;1-80 - -
  998.  
  999. copies standard input to standard output, replicating the first
  1000. eighty columns of each line twice.
  1001.  
  1002. 5.3  Delaminating Files by Separators: delamc
  1003.  
  1004.    This program delaminates standard input into several output
  1005. files according to the separator characters specified by the
  1006. string following the -t option.  It writes the fields in each
  1007. line to the corresponding output files as individual lines. If no
  1008. data occurs in the specified position for a given input line an
  1009. empty output line is written. This insures that all output files
  1010. contain the same number of lines as the input file.
  1011.  
  1012.    If - is used as an output file name, the corresponding field
  1013. is written to the standard output. If the -t option is not used,
  1014. an ascii horizontal tab character is assumed as the default field
  1015. separator.
  1016.  
  1017.    The use of delamc is illustrated by the following examples.
  1018. The command
  1019.  
  1020.         delamc labels opcodes operands
  1021.  
  1022. writes the fields of standard input, each of which is separated
  1023. by a tab character, to the output files labels, opcodes, and
  1024. operands.  The command
  1025.  
  1026.         delamc -t: scores names matric ps1 ps2 ps3
  1027.  
  1028. writes the fields of standard input, each of which are separated
  1029. by a colon, to the indicated output files.  The command
  1030.  
  1031.         delamc -t,: oldata f1 f2
  1032.  
  1033. separates the fields using either a comma or a colon.
  1034.  
  1035.  
  1036. 6.  Icon Program Utilities
  1037.  
  1038. 6.1  Icon Program Cross Reference: ipxref
  1039.  
  1040.    This program cross-references Icon programs. It lists the
  1041. occurrences of each variable by line number. Variables are listed
  1042. by procedure or separately as globals.  The options specify the
  1043. formatting of the output and whether or not to cross-reference
  1044. quoted strings and non-alphanumerics. Variables that are followed
  1045. by a left parenthesis are listed with an asterisk following the
  1046. name.  If a file is not specified, then standard input is cross-
  1047. referenced.
  1048.  
  1049. Options: The following options change the format defaults:
  1050.  
  1051.  
  1052.  
  1053.  
  1054.                              - 15 -
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.                             Programs
  1061.  
  1062.  
  1063.      -c n The column width per line number. The default is 4
  1064.           columns wide.
  1065.  
  1066.      -l n The starting column (i.e. left margin) of the line
  1067.           numbers.  The default is column 40.
  1068.  
  1069.      -w n The column width of the whole output line. The default
  1070.           is 80 columns wide.
  1071.  
  1072.    Normally only alphanumerics are cross-referenced. These
  1073. options expand what is considered:
  1074.  
  1075.      -q   Include quoted strings.
  1076.  
  1077.      -x   Include all non-alphanumerics.
  1078.  
  1079. Note: This program assumes the subject file is a valid Icon pro-
  1080. gram. For example, quotes are expected to be matched.
  1081.  
  1082. 6.2  Sort Icon Declarations: ipsort
  1083.  
  1084.    This program reads an Icon program and writes an equivalent
  1085. program with the procedures sorted alphabetically. Global, link,
  1086. and record declarations come first in the order they appear in
  1087. the original program.  The main procedure comes next followed by
  1088. the remaining procedures in alphabetical order.
  1089.  
  1090.    Comments and white space between declarations are attached to
  1091. the next following declaration.
  1092.  
  1093. Limitations: This program only recognizes declarations that start
  1094. at the beginning of a line.
  1095.  
  1096.    Comments and interline white space between declarations may
  1097. not come out as intended.
  1098.  
  1099. 6.3  Icon Program Splitting: ipsplit
  1100.  
  1101.    This progam reads an Icon program and writes each procedure to
  1102. a separate file. The output file names consist of the procedure
  1103. name with .icn appended.  If the -g option is specified, any glo-
  1104. bal, link, and record declarations are written to that file. Oth-
  1105. erwise they are written in the file for the procedure that
  1106. immediately follows them.
  1107.  
  1108.    Comments and white space between declarations are attached to
  1109. the next following declaration.
  1110.  
  1111. Notes: The program only recognizes declarations that start at the
  1112. beginning of lines.  Comments and interline white space between
  1113. declarations may not come out as intended.
  1114.  
  1115.    If the -g option is not specified, any global, link, or record
  1116. declarations that follow the last procedure are discarded.
  1117.  
  1118.  
  1119.  
  1120.                              - 16 -
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.                             Programs
  1127.  
  1128.  
  1129. 7.  Miscellaneous Utilities
  1130.  
  1131. 7.1  Line Lengths: ll
  1132.  
  1133.    This program prints the lengths of the shortest and longest
  1134. lines in files named on the command line.  If there is no command
  1135. line argument, the standard input is used.  The argument - may be
  1136. used to explicitly specify the standard input.
  1137.  
  1138. 7.2  Trimming Lines: trim
  1139.  
  1140.    This program copies lines from standard input to standard out-
  1141. put, truncating the lines at n characters and removing any trail-
  1142. ing blanks. The default value for n is 80.  For example,
  1143.  
  1144.         trim 70 <grade.txt >grade.fix
  1145.  
  1146. copies grade.txt to grade.fix, with lines longer than 70 charac-
  1147. ters truncated to 70 characters and the trailing blanks removed
  1148. from all lines.
  1149.  
  1150.    The -f option causes all lines to be n characters long by
  1151. adding blanks to short lines; otherwise, short lines are left as
  1152. is.
  1153.  
  1154. 7.3  Sorting Groups of Lines: grpsort
  1155.  
  1156.    This program sorts input containing ``records'' defined to be
  1157. groups of consecutive lines. Output is written to standard out-
  1158. put.  Each input record is separated by one or more repetitions
  1159. of a demarcation line (a line beginning with the separator
  1160. string).  The first line of each record is used as the key.
  1161.  
  1162.    If no separator string is specified on the command line, the
  1163. default is the empty string. Because all input lines are trimmed
  1164. of whitespace (blanks and tabs), empty lines are default demarca-
  1165. tion lines. The separator string specified can be an initial sub-
  1166. string of the string used to demarcate lines, in which case the
  1167. resulting partition of the input file may be different from a
  1168. partition created using the entire demarcation string.
  1169.  
  1170.    The -o option sorts the input file but does not produce the
  1171. sorted records.  Instead it lists the keys (in sorted order) and
  1172. line numbers defining the extent of the record associated with
  1173. each key.
  1174.  
  1175.    The use of grpsort is illustrated by the following examples.
  1176. The command
  1177.  
  1178.         grpsort "catscats" <x >y
  1179.  
  1180. sorts the file x, whose records are separated by lines containing
  1181. the string "catscats", into the file y placing a single line of
  1182. "catscats" between each output record. Similarly, the command
  1183.  
  1184.  
  1185.  
  1186.                              - 17 -
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.                             Programs
  1193.  
  1194.  
  1195.         grpsort "cats" <x >y
  1196.  
  1197. sorts the file x as before but assumes that any line beginning
  1198. with the string "cats" delimits a new record. This may or may not
  1199. divide the lines of the input file into a number of records dif-
  1200. ferent from the previous example.  In any case, the output
  1201. records will be separated by a single line of "cats".  Another
  1202. example is
  1203.  
  1204.         grpsort -o <bibliography >bibkeys
  1205.  
  1206. which sorts the file bibliography and produces a sorted list of
  1207. the keys and the extents of the associated records in bibkeys.
  1208. Each output key line is of the form:
  1209.  
  1210.         [s-e] key
  1211.  
  1212. where
  1213.  
  1214.         s     is the line number of the key line
  1215.         e     is the line number of the last line
  1216.         key   is the actual key of the record
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.  
  1241.  
  1242.  
  1243.  
  1244.  
  1245.  
  1246.  
  1247.  
  1248.  
  1249.  
  1250.  
  1251.  
  1252.                              - 18 -
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.                            Procedures
  1259.  
  1260.  
  1261. Introduction
  1262.  
  1263.    Collections of translated procedures are in the distribution
  1264. directory procs.  These files can be linked into other programs.
  1265. For example, if the Icon program library resides in
  1266. /usr/icon/v6/ipl, the procedures in gener.icn can be linked with
  1267. an Icon program by using the link declaration 2:
  1268.  
  1269.         link "/usr/icon/v6/ipl/procs/gener"
  1270.  
  1271. The IPATH environment variable [2] can be used to have the direc-
  1272. tory containing the translated procedures searched automatically.
  1273. For example, if IPATH is set to /usr/icon/v6/ipl/procs, the link
  1274. declaration need only be
  1275.  
  1276.         link gener
  1277.  
  1278.  
  1279. 1.  Math Procedures: math
  1280.  
  1281. The following procedures compute standard trigonometric func-
  1282. tions.  The arguments are in radians.
  1283.  
  1284.      sin(x)         sine of x
  1285.  
  1286.      cos(x)         cosine of x
  1287.  
  1288.      tan(x)         tangent of x
  1289.  
  1290.      asin(x)        arc sine of x in the range -pi/2 to pi/2
  1291.  
  1292.      acos(x)        arc cosine of x in the range 0 to pi
  1293.  
  1294.      atan(x)        arc tangent of x in the range -pi/2 to pi/2
  1295.  
  1296.      atan2(y,x)     arc tangent of x/y in the range -pi to pi
  1297.  
  1298. The following procedures convert from degrees to radians and con-
  1299. versely:
  1300.  
  1301.      dtor(d)        radian equivalent of d
  1302.  
  1303.      rtod(r)        degree equivalent of r
  1304.  
  1305. The following additional procedures are available:
  1306.  
  1307.  
  1308.                           
  1309. 2In MS-DOS, backslashes can be used in place of  slashes  in
  1310. such link declarations, but they must be escaped, as in
  1311.  
  1312.         link "\\usr\\icon\\v6\\ipl\\procs\\gener"
  1313.  
  1314.  
  1315.  
  1316.  
  1317.  
  1318.                              - 19 -
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.                            Procedures
  1325.  
  1326.  
  1327.      sqrt(x)        square root of x
  1328.  
  1329.      exp(x)         exponential function of x
  1330.  
  1331.      log(x)         natural logarithm of x
  1332.  
  1333.      log10(x)       base-10 logarithm of x
  1334.  
  1335.      floor(x)       largest integer not greater than x
  1336.  
  1337.      ceil(x)        smallest integer nor less than x
  1338.  
  1339. Failure Conditions: asin(x) and acos(x) fail if the absolute
  1340. value of x is greater than one. sqrt(x), log(x), and log10(x)
  1341. fail if x is less than zero.
  1342.  
  1343.  
  1344. 2.  Bit Operations: bitops
  1345.  
  1346. The following procedures perform operations on characters strings
  1347. of zeros and ones (``bit strings'').
  1348.  
  1349.      and(b1,b2)     logical ``and'' of b1 and b2
  1350.  
  1351.      bitstring(i)   convert integer i to bit string
  1352.  
  1353.      bsum(b1,b2)    arithmetic sum of b1 and b2 (used by other
  1354.                     procedures)
  1355.  
  1356.      decimal(b)     convert b to integer
  1357.  
  1358.      exor(b1,b2)    ``exclusive-or'' of b1 and b2
  1359.  
  1360.      neg(b)         negation of b
  1361.  
  1362.      or(b1,b2)      logical ``or'' of b1 and b2
  1363.  
  1364. Note: If i in bitstring(i) is negative, the value produced is the
  1365. corresponding unsigned 32-bit bit string.
  1366.  
  1367. Bugs: Integer values that exceed those allowable in Icon may pro-
  1368. duce bogus results or spurious diagnostics.
  1369.  
  1370.  
  1371. 3.  Radix Conversions: radcon
  1372.  
  1373. The following procedures convert numbers from one radix to
  1374. another. The letters from a to z are used for ``digits'' greater
  1375. than 9. All the conversion procedures fail if the conversion can-
  1376. not be made.
  1377.  
  1378.      exbase10(i,j)  convert base-10 integer i to base j
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384.                              - 20 -
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.                            Procedures
  1391.  
  1392.  
  1393.      inbase10(s,i)  convert base-i integer s to base 10
  1394.  
  1395.      radcon(s,i,j)  convert base-i integer s to base j
  1396.  
  1397. Limitation: The maximum base allowed is 36.
  1398.  
  1399.  
  1400. 4.  Complex Arithmetic: complex
  1401.  
  1402. The following procedures perform operations on complex numbers.
  1403.  
  1404.      complex(r,i)   create complex number with real part r and
  1405.                     imaginary part i
  1406.  
  1407.      cpxadd(x1,x2)  add complex numbers x1 and x2
  1408.  
  1409.      cpxdiv(x1,x2)  divide complex number x1 by complex number x2
  1410.  
  1411.      cpxmul(x1,x2)  multiply complex number x1 by complex number
  1412.                     x2
  1413.  
  1414.      cpxsub(x1,x2)  subtract complex number x2 from complex
  1415.                     number x1
  1416.  
  1417.      cpxstr(x)      convert complex number x to string represen-
  1418.                     tation
  1419.  
  1420.      strcpx(s)      convert string representation s of complex
  1421.                     number to complex number
  1422.  
  1423.  
  1424. 5.  Collated Strings: collate
  1425.  
  1426. These procedures collate (interleave) respective characters of
  1427. two strings and decollate such strings by selecting every other
  1428. character of a string.  produce a string consisting of inter-
  1429. leaved characters of s1 and s2.
  1430.  
  1431.      collate(s1,s2) collate the characters of s1 and s2.  For
  1432.                     example,
  1433.                          collate("abc","def")
  1434.                     produces "adbecf".
  1435.  
  1436.      decollate(s,i) produce a string consisting of every other
  1437.                     character of s. If i is odd, the odd-numbered
  1438.                     characters are selected, while if i is even,
  1439.                     the even-numbered characters are selected.
  1440.  
  1441. Diagnostics: Run-time error 208 occurs if the arguments to col-
  1442. late are not of the same size.
  1443.  
  1444.  
  1445.  
  1446.  
  1447.  
  1448.  
  1449.  
  1450.                              - 21 -
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.                            Procedures
  1457.  
  1458.  
  1459. 6.  Emphasized Text: bold
  1460.  
  1461. These procedures produce text with interspersed characters suit-
  1462. able for printing to produce the effect of boldface (by over-
  1463. striking) and underscoring (using backspaces).
  1464.  
  1465.      bold(s)        bold version of s
  1466.  
  1467.      uscore(s)      underscored version of s
  1468.  
  1469.  
  1470. 7.  Shuffling: shuffle
  1471.  
  1472. The procedure shuffle(x) shuffles a string or list. In the case
  1473. that x is a string, a corresponding string with the characters
  1474. randomly rearranged is produced. In the case that x is a list,
  1475. the values in the list are randomly rearranged.
  1476.  
  1477.  
  1478. 8.  Segmented Strings: segment
  1479.  
  1480. The procedure segment(s,c) generates consecutive substrings of s
  1481. consisting of characters that respectively do/do not occur in c.
  1482. For example,
  1483.  
  1484.         segment("Not a sentence.",&lcase ++ &ucase)
  1485.  
  1486. generates
  1487.  
  1488.         "Not"
  1489.         " "
  1490.         "a"
  1491.         " "
  1492.         "sentence"
  1493.         "."
  1494.  
  1495.  
  1496.  
  1497. 9.  String Utilities: strutil
  1498.  
  1499. These procedures perform simple operations on strings.
  1500.  
  1501.      compress(s,c)  compress consecutive occurrences of charac-
  1502.                     ters in c that occur in s
  1503.  
  1504.      delete(s,c)    delete all occurrences of characters in c
  1505.                     that occur in s
  1506.  
  1507.      rotate(s,i)    rotate s i characters to the left (negative i
  1508.                     produces rotation to the right); the default
  1509.                     value of i is 1
  1510.  
  1511.  
  1512.  
  1513.  
  1514.  
  1515.  
  1516.                              - 22 -
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.                            Procedures
  1523.  
  1524.  
  1525. 10.  Structure Utilities: structs
  1526.  
  1527. These procedures manipulate trees and acyclic graphs (dags).  The
  1528. structures are represented with lists.  See [1].
  1529.  
  1530.      depth(t)  compute maximum depth of tree t
  1531.  
  1532.      eq(x,y)   compare list structures x and y
  1533.  
  1534.      ldag(s)   construct a dag from the string s
  1535.  
  1536.      ltree(s)  construct a tree from the string s
  1537.  
  1538.      stree(t)  construct a string from the tree t
  1539.  
  1540.      tcopy(t)  copy tree t
  1541.  
  1542.      teq(t1,t2)compare trees t1 and t2
  1543.  
  1544.      visit(t)  visit, in preorder, the nodes of the tree t
  1545.  
  1546. Note: The procedure ldag has a second argument that is used on
  1547. internal recursive calls; a second argument must not be supplied
  1548. by the user.
  1549.  
  1550.  
  1551. 11.  Icon Literal Escapes: escape
  1552.  
  1553. The procedure escape(s) produces a string in which Icon quoted
  1554. literal escape conventions in s are replaced by the corresponding
  1555. characters.  For example, escape("\\143\\141\\164") produces the
  1556. string "cat".
  1557.  
  1558.  
  1559. 12.  Images of Icon Values: image
  1560.  
  1561. The procedure Image(x) produces a string image of the value x.
  1562. The value produced is a generalization of the value produced by
  1563. the Icon function image(x), providing detailed information about
  1564. structures.
  1565.  
  1566.    Tags are used to uniquely identify structures. A tag consists
  1567. of a letter identifying the type followed by an integer. The tag
  1568. letters are L for lists, R for records, S for sets, and T for
  1569. tables. The first time a structure is encountered, it is imaged
  1570. as the tag followed by a colon, followed by a representation of
  1571. the structure. If the same structure is encountered again, only
  1572. the tag is given.
  1573.  
  1574.    An example is
  1575.  
  1576.  
  1577.  
  1578.  
  1579.  
  1580.  
  1581.  
  1582.                              - 23 -
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588.                            Procedures
  1589.  
  1590.  
  1591.    a := ["x"]
  1592.    push(a,a)
  1593.    t := table()
  1594.    push(a,t)
  1595.    t[a] := t
  1596.    t["x"] := []
  1597.    t[t] := a
  1598.    write(Image(t))
  1599.  
  1600. which produces
  1601.  
  1602.    T1:["x"->L1:[],L2:[T1,L2,"x"]->T1,T1->L2]
  1603.  
  1604. Note that a table is represented as a list of entry and assigned
  1605. values separated by ->.
  1606.  
  1607.  
  1608. 13.  List Mapping: lmap
  1609.  
  1610. The procedure lmap(a1,a2,a3) maps elements of a1 according to a2
  1611. and a3.  This procedure is the analog for lists of the built-in
  1612. string-mapping function map(s1,s2,s3). Elements in a1 that are
  1613. the same as elements in a2 are mapped into the corresponding ele-
  1614. ments of a3. For example, given the lists
  1615.  
  1616.    a1 := [1,2,3,4]
  1617.    a2 := [4,3,2,1]
  1618.    a3 := ["a","b","c","d"]
  1619.  
  1620. then
  1621.  
  1622.    lmap(a1,a2,a3)
  1623.  
  1624. changes a1 to
  1625.  
  1626.    ["d","c","b","a"]
  1627.  
  1628. Note that the value of a1 is modified.
  1629.  
  1630.    Lists that are mapped can have any kinds of elements. The
  1631. operation
  1632.  
  1633.    x === y
  1634.  
  1635. is used to determine if elements x and y are equivalent.
  1636.  
  1637.    All cases in lmap are handled as they are in map, except that
  1638. no defaults are provided for omitted arguments. As with map, lmap
  1639. can be used for transposition as well as substitution.
  1640.  
  1641. Warning: If lmap is called with the same lists a2 and a3 as in
  1642. the immediately preceding call, the same mapping is performed,
  1643. even if the values in a2 and a3 have been changed. This improves
  1644. performance, but it may cause unexpected effects.
  1645.  
  1646.  
  1647.  
  1648.                              - 24 -
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.                            Procedures
  1655.  
  1656.  
  1657. Comments: It is easy to change lmap to produce a new list instead
  1658. of modifying a1; this is a good exercise for beginning Icon pro-
  1659. grammers. The ``caching'' of the mapping table based on a2 and a3
  1660. also can be removed easily to avoid the potential problem men-
  1661. tioned in the warning above.
  1662.  
  1663.  
  1664. 14.  Snapshots of Scanning: snapshot
  1665.  
  1666. The procedure snapshot() writes a snapshot of the state of string
  1667. scanning, showing the value of &subject and &pos. For example,
  1668.  
  1669.    "((a+b)-delta)/(c*d))" ? {
  1670.       tab(bal('+-/*'))
  1671.       snapshot()
  1672.       }
  1673.  
  1674. produces
  1675.      -------------------------------------
  1676.      |                                   |
  1677.      | &subject = "((a+b)-delta)/(c*d))" |
  1678.      |                          |        |
  1679.      -------------------------------------
  1680.  
  1681. Note that the bar showing the &pos is positioned under the &posth
  1682. character (actual positions are between characters).  If &pos is
  1683. at the end of &subject, the bar is positioned under the quotation
  1684. mark delimiting the subject. For example,
  1685.  
  1686.    "abcdefgh" ? (tab(0) & snapshot())
  1687.  
  1688. produces
  1689.      -------------------------
  1690.      |                       |
  1691.      | &subject = "abcdefgh" |
  1692.      |                     | |
  1693.      -------------------------
  1694.  
  1695. Escape sequences are handled properly. For example,
  1696.  
  1697.    "abc\tdef\nghi" ? (tab(upto('\n')) & snapshot())
  1698.  
  1699. produces
  1700.      ------------------------------
  1701.      |                            |
  1702.      | &subject = "abc\tdef\nghi" |
  1703.      |                     |      |
  1704.      ------------------------------
  1705.  
  1706.  
  1707.  
  1708.  
  1709.  
  1710.  
  1711.  
  1712.  
  1713.  
  1714.                              - 25 -
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720.                            Procedures
  1721.  
  1722.  
  1723. 15.  Miscellaneous Generators: gener
  1724.  
  1725. These procedures generate sequences of results.
  1726.  
  1727.      hex()          sequence of hexadecimal codes for numbers
  1728.                     from 0 to 255
  1729.  
  1730.      label(s,i)     sequence of labels with prefix s starting at
  1731.                     i
  1732.  
  1733.      octal()        sequence of octal codes for numbers from 0 to
  1734.                     255
  1735.  
  1736.      star(s)        sequence consisting of the closure of s
  1737.                     starting with the empty string and continuing
  1738.                     in lexical order as given in s
  1739.  
  1740.  
  1741. 16.  Result Sequences: seqimage
  1742.  
  1743. The procedure Seqimage{e,i,j} produces a string image of the
  1744. result sequence for the expression e. The first i results are
  1745. printed. If i is omitted, there is no limit. If there are more
  1746. than i results for e, ellipses are provided in the image after
  1747. the first i.  If j is specified, at most j results from the end
  1748. of the sequence are printed after the ellipses.  If j is omitted,
  1749. only the first i results are produced.
  1750.  
  1751. For example, the expressions
  1752.  
  1753.    Seqimage{1 to 12}
  1754.    Seqimage{1 to 12,10}
  1755.    Seqimage{1 to 12,6,3}
  1756.  
  1757. produce, respectively,
  1758.  
  1759.    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
  1760.    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...}
  1761.    {1, 2, 3, 4, 5, 6, ..., 10, 11, 12}
  1762.  
  1763.  
  1764. Warning: If j is not omitted and e has a infinite result
  1765. sequence, Seqimage does not terminate.
  1766.  
  1767.  
  1768. 17.  SNOBOL4 Pattern Matching: patterns
  1769.  
  1770. These procedures provide procedural equivalents for most SNOBOL4
  1771. patterns and some extensions. See [9-11].  Procedures and their
  1772. pattern equivalents are:
  1773.  
  1774.      Any(s)         ANY(S)
  1775.  
  1776.  
  1777.  
  1778.  
  1779.  
  1780.                              - 26 -
  1781.  
  1782.  
  1783.  
  1784.  
  1785.  
  1786.                            Procedures
  1787.  
  1788.  
  1789.      Arb()          ARB
  1790.  
  1791.      Arbno(p)       ARBNO(P)
  1792.  
  1793.      Arbx(i)        ARB(I)
  1794.  
  1795.      Bal()          BAL
  1796.  
  1797.      Break(s)       BREAK(S)
  1798.  
  1799.      Breakx(s)      BREAKX(S)
  1800.  
  1801.      Cat(p1,p2)     P1 P2
  1802.  
  1803.      Discard(p)     /P
  1804.  
  1805.      Exog(s)        \S
  1806.  
  1807.      Find(s)        FIND(S)
  1808.  
  1809.      Len(i)         LEN(I)
  1810.  
  1811.      Limit(p,i)     P \ i
  1812.  
  1813.      Locate(p)      LOCATE(P)
  1814.  
  1815.      Marb()         longest-first ARB
  1816.  
  1817.      Notany(s)      NOTANY(S)
  1818.  
  1819.      Pos(i)         POS(I)
  1820.  
  1821.      Replace(p,s)   P   S
  1822.  
  1823.      Rpos(i)        RPOS(I)
  1824.  
  1825.      Rtab(i)        RTAB(I)
  1826.  
  1827.      Span(s)        SPAN(S)
  1828.  
  1829.      String(s)      S
  1830.  
  1831.      Succeed()      SUCCEED
  1832.  
  1833.      Tab(i)         TAB(I)
  1834.  
  1835.      Xform(f,p)     F(P)
  1836.  
  1837.    The following procedures relate to the application and control
  1838. of pattern matching:
  1839.  
  1840.      Apply(s,p)     S ? P
  1841.  
  1842.  
  1843.  
  1844.  
  1845.  
  1846.                              - 27 -
  1847.  
  1848.  
  1849.  
  1850.  
  1851.  
  1852.                            Procedures
  1853.  
  1854.  
  1855.      Mode()         anchored or unanchored matching (see Anchor
  1856.                     and Float)
  1857.  
  1858.      Anchor()       &ANCHOR = 1  if Mode := Anchor
  1859.  
  1860.      Float()        &ANCHOR = 0  if Mode := Float
  1861.  
  1862. In addition to the procedures above, the following expressions
  1863. can be used:
  1864.  
  1865.      p1() | p2()    P1 | P2
  1866.  
  1867.      v <- p()       P . V  (approximate)
  1868.  
  1869.      v := p()       P $ V  (approximate)
  1870.  
  1871.      fail           FAIL
  1872.  
  1873.      =s             S  (in place of String(s))
  1874.  
  1875.      p1() || p2()   P1 P2  (in place of Cat(p1,p2))
  1876.  
  1877. Using this system, most SNOBOL4 patterns can be satisfactorily
  1878. transliterated into Icon procedures and expressions. For example,
  1879. the pattern
  1880.  
  1881.    SPAN("0123456789") $ N "H" LEN(*N) $ LIT
  1882.  
  1883. can be transliterated into
  1884.  
  1885.    (n <- Span('0123456789')) || ="H" ||
  1886.       (lit <- Len(n))
  1887.  
  1888. Concatenation of components is necessary to preserve the
  1889. pattern-matching properties of SNOBOL4.  See the documents refer-
  1890. enced above for details and limitations.
  1891.  
  1892. Caveats: Simulating SNOBOL4 pattern matching using the procedures
  1893. above is inefficient.
  1894.  
  1895.  
  1896. 18.  Defined Control Operations: pdco
  1897.  
  1898. These procedures use co-expressions to used to model the built-in
  1899. control structures of Icon and also provide new ones. See [12].
  1900.  
  1901.      Alt{e1,e2}         models e1 | e2
  1902.  
  1903.      Colseq{e1,e2, ...} produces results of e1, e2, ... alter-
  1904.                         nately
  1905.  
  1906.      Comseq{e1,e2}      compares result sequences of e1 and e2
  1907.  
  1908.  
  1909.  
  1910.  
  1911.  
  1912.                              - 28 -
  1913.  
  1914.  
  1915.  
  1916.  
  1917.  
  1918.                            Procedures
  1919.  
  1920.  
  1921.      Cond{e1,e2, ...}   models the generalized Lisp conditional
  1922.  
  1923.      Every{e1,e2}       models every e1 do e2
  1924.  
  1925.      Galt{e1,e2, ...}   models generalized alternation: e1 | e2 |
  1926.                         ...
  1927.  
  1928.      Lcond{e1,e2, ...}  models the Lisp conditional
  1929.  
  1930.      Limit{e1,e2}       models e1 \ e2
  1931.  
  1932.      Ranseq{e1,e2, ...} produces results of e1, e2, ... at random
  1933.  
  1934.      Repalt{e}          models |e
  1935.  
  1936.      Resume{e1,e2,e3}   models every e1 \ e2 do e3
  1937.  
  1938.      Select{e1,e2}      produces results from e1 by position
  1939.                         according to e2
  1940.  
  1941. Comments: Because of the handling of the scope of local identif-
  1942. iers in co-expressions, expressions in programmer-defined control
  1943. operations cannot communicate through local identifiers.  Some
  1944. constructions, such as break and return, cannot be used in argu-
  1945. ments to programmer-defined control operations.
  1946.  
  1947.  
  1948. 19.  Defined Control Regimes: pdae
  1949.  
  1950. These procedures use co-expressions to model the built-in argu-
  1951. ment evaluation regime of Icon and also provide new ones.  See
  1952. [13].
  1953.  
  1954.      Allpar{e1,e2, ...}   parallel evaluation with last result
  1955.                           used for short sequences
  1956.  
  1957.      Extract{e1,e2, ...}  extract results of even-numbered argu-
  1958.                           ments according to odd-numbered values
  1959.  
  1960.      Lifo{e1,e2, ...}     models standard Icon ``lifo'' evalua-
  1961.                           tion
  1962.  
  1963.      Parallel{e1,e2, ...} parallel evaluation terminating on
  1964.                           shortest sequence
  1965.  
  1966.      Reverse{e1,e2, ...}  left-to-right reversal of lifo evalua-
  1967.                           tion
  1968.  
  1969.      Rotate{e1,e2, ...}   parallel evaluation with shorter
  1970.                           sequences re-evaluated
  1971.  
  1972.      Simple{e1,e2, ...}   simple evaluation with only success or
  1973.                           failure
  1974.  
  1975.  
  1976.  
  1977.  
  1978.                              - 29 -
  1979.  
  1980.  
  1981.  
  1982.  
  1983.  
  1984.                            Procedures
  1985.  
  1986.  
  1987. Comments: Because of the handling of the scope of local identif-
  1988. iers in co-expressions, expressions in programmer-defined argu-
  1989. ment evaluation regimes cannot communicate through local identif-
  1990. iers.  Some constructions, such as break and return, cannot be
  1991. used in arguments to programmer-defined argument evaluation
  1992. regimes.
  1993.  
  1994. At most 10 arguments can be used in the invocation of a
  1995. programmer-defined argument evaluation regime. This limit can be
  1996. increased by modifying Call, a utility procedure that is
  1997. included.
  1998.  
  1999.  
  2000.  
  2001.  
  2002.  
  2003.  
  2004.  
  2005.  
  2006.  
  2007.  
  2008.  
  2009.  
  2010.  
  2011.  
  2012.  
  2013.  
  2014.  
  2015.  
  2016.  
  2017.  
  2018.  
  2019.  
  2020.  
  2021.  
  2022.  
  2023.  
  2024.  
  2025.  
  2026.  
  2027.  
  2028.  
  2029.  
  2030.  
  2031.  
  2032.  
  2033.  
  2034.  
  2035.  
  2036.  
  2037.  
  2038.  
  2039.  
  2040.  
  2041.  
  2042.  
  2043.  
  2044.                              - 30 -
  2045.  
  2046.  
  2047.  
  2048.  
  2049.  
  2050.  
  2051.  
  2052.  
  2053. References
  2054.  
  2055.  
  2056. 1.   R. E. Griswold and M. T. Griswold, The Icon Programming
  2057.      Language, Prentice-Hall, Inc., Englewood Cliffs, NJ, 1983.
  2058.  
  2059. 2.   R. E. Griswold, W. H. Mitchell and J. O'Bagy, Version 6 of
  2060.      Icon, The Univ. of Arizona Tech. Rep. 86-10b, 1986.
  2061.  
  2062. 3.   R. E. Griswold, Version 6 of Icon for MS-DOS, The Univ. of
  2063.      Arizona Tech. Rep., 1986.
  2064.  
  2065. 4.   G. M. Townsend, Using Version 6 of Icon Under VMS, The Univ.
  2066.      of Arizona Tech. Rep., 1986.
  2067.  
  2068. 5.   R. E. Griswold, Programming in Icon; Problems and Solutions
  2069.      from the Icon Newsletter, The Univ. of Arizona Tech. Rep.
  2070.      86-2a, 1986.
  2071.  
  2072. 6.   C. S. Wetherwell, ``Probablistic Languages: A Review and
  2073.      Some Open Questions'', Computing Surveys 12, 4 (1980), 362-
  2074.      379.
  2075.  
  2076. 7.   A. Salomaa, Formal Languages, Academic Press, 1973.
  2077.  
  2078. 8.   D. B. Anderson and M. R. Sleep, ``Uniform Random Generation
  2079.      of Balanced Parenthesis Strings'', ACM Trans. Prog. Lang.
  2080.      and Systems 2, 1 (1980), 122-128.
  2081.  
  2082. 9.   R. E. Griswold, Pattern Matching in Icon, The Univ. of
  2083.      Arizona Tech. Rep. 80-25, 1980.
  2084.  
  2085. 10.  R. E. Griswold, Models of String Pattern Matching, The Univ.
  2086.      of Arizona Tech. Rep. 81-6, 1981.
  2087.  
  2088. 11.  A. C. Fleck, ``Formal Models for String Patterns'', in
  2089.      Current Trends in Programming Methodology; Data Structuring,
  2090.      vol. IV, Prentice-Hall, Inc., Englewood Cliffs, NJ, 1978,
  2091.      216-240.
  2092.  
  2093. 12.  R. E. Griswold and M. Novak, ``Programmer-Defined Control
  2094.      Operations'', Computer J. 26, 2 (May 1983), 175-183.
  2095.  
  2096. 13.  M. Novak and R. E. Griswold, Programmer-Defined Argument
  2097.      Evaluation Regimes, The Univ. of Arizona Tech. Rep. 82-16,
  2098.      1982.
  2099.  
  2100.  
  2101.  
  2102.  
  2103.  
  2104.  
  2105.  
  2106.  
  2107.  
  2108.  
  2109.  
  2110.                              - 31 -
  2111.  
  2112.  
  2113.