home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / teach.zip / TEACH7A.ATF < prev    next >
Text File  |  1997-09-18  |  74KB  |  908 lines

  1. XNÉIO 0 1                                                                      °
  2. XNÉCT 0 1E²13                                                                  °
  3. XCÉFC 1 6 .,*0_²                                                               °
  4. XNÉRL 0 16807                                                                  °
  5. XCÉPR 1 1                                                                      °
  6. XCÉLX 1 5 TEACH                                                                °
  7. XAC╜(1)(2)(Γ3 4)(5 6)                                                          °
  8. *(1997 9 14 12 30 44 504)                                                      °
  9. XFDISCLAIMER ÉFX 'DISCLAIMER' 'Σ Copyright statement' 'disclaimer'             °
  10. *(1996 4 6 16 3 9 216)                                                         °
  11.  FDISPLAY ÉFX 'D╜S DISPLAY A;ÉIO;R;C;HL;HC;HT;HB;VL;VB;V;W;N;B'                °
  12.   'Σ DISPLAY A GENERAL ARRAY IN PICTORIAL FORM'                                °
  13.   'Σ  NORMAL CALL IS MONADIC.  DYADIC CALL USED ONLY IN'                       °
  14.   'Σ    RECURSION TO SPECIFY DISPLAY RANK, SHAPE, AND DEPTH.' 'ÉIO╜0'          °
  15.   '»(0=ÉNC ''S'')/''S╜µA''' 'R╜╞µ,S                   Σ PSEUDO RANK.'          °
  16.   'C╜''┐┌└┘''                 Σ UR, UL, LL, AND LR CORNERS.'                   °
  17.   'HL╜''─''                   Σ HORIZONTAL LINE.'                              °
  18.   'HC╜HL,''Θ╕'',HL,''~+ε''      Σ HORIZONTAL BORDERS.'                         °
  19.   'HT╜HC[(0<R)⌡1+0<╞²1╞,S]' 'ΣW╜,0╧■╞0µΓ(1⌐µA)╞A'                              °
  20.   'HB╜HC[3+3╛(''2⌡~A╧«A'' ÉEA ''1+╞ε0⌡(1⌐⌡/µA)╞,A'')+3⌡1<µµS]'                 °
  21.   'VL╜''│''                   Σ VERTICAL LINE.'                                °
  22.   'VB╜VL,''Φ╟''               Σ VERTICAL BORDER.'                              °
  23.   'V╜VB[(1<R)⌡1+0<²1╞²1╟,S]'                                                   °
  24.   '»(0εµA)/''A╜(1⌐µA)µΓ╞A''   Σ SHOW PROTOTYPE OF EMPTIES.'                    °
  25.   '╕(1<╧A)/GEN' '╕(2<µµA)/D3'                                                  °
  26.   'D╜«A                     Σ SIMPLE ARRAYS.' 'W╜1╞µD╜(²2╞1 1,µD)µD'           °
  27.   'N╜²1+1╟µD' '╕(0=µµA)/SS'                                                    °
  28.   'D╜(C[1],V,((W-1)µVL),C[2]),((HT,NµHL),[0]D,[0]HB,NµHL),C[0],(WµVL),C[       °
  29.  3]' '╕0' 'SS:HB╜((0 '' '')=╞0µΓA)/'' -'''                                     °
  30.   'D╜'' '',('' '',[0]D,[0]HB,Nµ'' ''),'' ''' '╕0'                              °
  31.   'GEN:D╜«DISPLAY■A          Σ ENCLOSED ...' 'N╜Dδ.⌠'' '''                     °
  32.   'D╜(Nδ~1ΦN)≡D' 'D╜(δ≡~''  ''╤D)/D' 'D╜((1,µS)µS)DISPLAY D'                   °
  33.   '╕(2≥µ,S)╟D3E,0' 'D3:D╜0 ²1╟0 1╟«ΓA         Σ MULT-DIMENSIONAL ...'          °
  34.   'W╜1╞µD' 'N╜²1+1╟µD'                                                         °
  35.   'D╜(C[1],V,((W-1)µVL),C[2]),((HT,NµHL),[0]D,[0]HB,NµHL),C[0],(WµVL),C[       °
  36.  3]' 'D3E:N╜²2+µ,S'                                                            °
  37. X 'V╜C[Nµ1],[0]VB[1+0<²2╟,S],[0](((²3+╞µD),N)µVL),[0]C[Nµ2]' 'D╜V,D'           °
  38. *(1996 4 6 16 3 9 216)                                                         °
  39.  FEXIT ÉFX 'EXIT' 'Σ Exit from function' '''To log off type:   )OFF'''         °
  40. X '╕'                                                                          °
  41. *(1996 4 6 16 3 9 216)                                                         °
  42.  FGO ÉFX 'GO;T;E;B' 'Σ Expression driver' 'L0:B╜E╜''''' 'æ╜''      '''         °
  43.   'T╜æ' '╕(^/'' ''=T)/L0' '╕((^/'')OFF ''=5╞6╟T)doif ''EXIT'')/0'              °
  44.   '╕(('':''εT)doif ''B╜evaldd (+/^\'''' ''''=T)╟T'')/L0'                       °
  45.   '''E╜ÉEM'' ÉEA T' '╕(0=µ,E)/L0' '╕B/L0'                                      °
  46.   '''This is not a valid APL2 expression''' 'æ╜''*'''                          °
  47. X '╕(''?''⌠╞1╟æ)/L0' 'E' '╕L0'                                                 °
  48. *(1997 8 15 11 56 12 476)                                                      °
  49.  FHELP ÉFX 'HELP;N;I;T' 'Σ Help to student' '''WHAT TO DO'',r'                 °
  50.   '''  ° To get out of the lesson''' '''       ENTER: EXIT'''                  °
  51.   '''  ° To log off APL2'''                                                    °
  52.   '''       FIRST, ENTER: EXIT  THEN ENTER: )OFF'''                            °
  53.   '''  ° To get help''' '''       ENTER: HELP'''                               °
  54.   '''  ° When you see the prompt on a blank line'''                            °
  55.   '''       ENTER AN APL2 EXPRESSION - OR JUST PRESS: ENTER''' 'do'            °
  56.   '''  ° If you get this line'',r'                                             °
  57.   '''This is not a valid APL2 expression'',r,''*'',r'                          °
  58.   '''        YOU CAN EITHER''' '''           A. PRESS: ENTER'''                °
  59.   '''           B. PRESS: ? and then ENTER to see what was incorrect'''        °
  60.   '''              in your expression causing that response'',r' 'do'          °
  61.   '''HINTS'',r'                                                                °
  62.   '''This lesson is made up of '',(«N╜7),'' components named TEACHx, whe       °
  63.  re''' '''the x stands for a digit:'',r' 'I╜0'                                 °
  64.   'L0:T╜''TEACH'',(«I╜I+1)' 'T,'' '',1╟notb(ÉCR T)[2;]' '╕(N>I)/L0'            °
  65.   'do'                                                                         °
  66.   '''You may review either of these components separately. To do that,''       °
  67.  '                                                                             °
  68.   '''first enter EXIT (and RETURN), then enter the name of the lesson.''       °
  69.  ' '''component (e.g. TEACH4).'',r'                                            °
  70.   '''To re-start the lesson, just enter TEACH'',r'                             °
  71.   '''When the screen fills up, it is a good idea to move the cursor to''       °
  72.  ' '''the start of a convenient paragraph, and press ENTER'',r'                °
  73.   '''You may also wish to press PAGE UP to review the prevous pages.'''        °
  74.   'do' '''RESOURCES'',r'                                                       °
  75.   '''You may also enter ONE of the following words at a time'',r'              °
  76. X '''REVIEW  To review the contents of the previous lesson''' 'endd'           °
  77. XANULL╜0µ0                                                                     °
  78. XCPHRASE 1 34 Once upon a time there was an elf.                               °
  79. *(1997 5 29 12 6 2 284)                                                        °
  80.  FREVIEW ÉFX 'REVIEW' 'Σ Review of lesson 6'                                   °
  81.   '''APL2 supports a variety of data structures. So far we have learned        °
  82.  that'''                                                                       °
  83.   '''elements of data can be organized in multi-dimensional box like arr       °
  84.  ays.'''                                                                       °
  85.   '''Such arrays can be described by specifying their dimensions. For'''       °
  86.   '''example, a vector has only one dimension, namely a length. A table'       °
  87.  ''                                                                            °
  88.   '''can be described by the number of rows and columns. A list of table       °
  89.  s''' '''is a table that is repeated in a third dimension.'',r'                °
  90.   '''One can readily calculate the total number of scalar (rank 0 array)       °
  91.  ''' '''components in such tabular data with the expression "⌡/µ"'',r'         °
  92.   'show ''A╜3 4 5µ∞6'' ''⌡/µA'''                                               °
  93.   '''The number of axes of an APL2 data object is called its RANK. This'       °
  94.  ''                                                                            °
  95.   '''number of dimensions, however, must not be confused with another'''       °
  96.   '''type of DIMENSION, namely that of data. For example a vector of len       °
  97.  gth''' '''N may be thought of as a point in N dimensional space.'''           °
  98.   'do'                                                                         °
  99.   '''While a scalar has no dimensions (rank 0), a vector may have zero''       °
  100.  '                                                                             °
  101.   '''length (null vector). The rank of a null vector is one because it''       °
  102.  '                                                                             °
  103.   '''is a vector. In general, the rank of any APL2 object can be any'''        °
  104.   '''positive integer or zero. (There is an upper limit to this number).       °
  105.  '''                                                                           °
  106.   '''The shape of any APL2 object is a vector whose components can be an       °
  107.  y'''                                                                          °
  108.   '''positive number or zero. (Here too there is a practical upper limit       °
  109.  .)''' 'do'                                                                    °
  110.   '''Reduction and scan of arrays is applied to the last axis. However,        °
  111.  there'''                                                                      °
  112.   '''are ways to modify reduction and scan. The symbols ≡ and ± force re       °
  113.  duction'''                                                                    °
  114.   '''and scan to apply to the FIRST axis. There is also a bracket notati       °
  115.  on'''                                                                         °
  116.   '''to force these two operators to be applied to intermediate axes.'''       °
  117. X 'endd'                                                                       °
  118. *(1997 5 31 11 59 20 572)                                                      °
  119.  FRULE ÉFX 'V RULE W' 'Σ Display rules' '╕(2=ÉNC ''V'')/L0'                    °
  120. X '2 rule W ╪ ╕0' 'L0:V rule W'                                                °
  121. *(1997 7 4 11 46 51 544)                                                       °
  122.  FTEACH ÉFX 'TEACH' 'Σ Start seventh lesson in APL2 by Zdenek V JIZBA'         °
  123.   'exit ''TEACH''' 'initialize' 'TEACH1 Σ Nested arrays and depth'             °
  124.   'TEACH2 Σ APL2 syntax' 'TEACH3 Σ Rank and depth; the pick function'          °
  125.   'TEACH4 Σ Using pick on nested arrays'                                       °
  126.   'TEACH5 Σ The enclose with axis' 'TEACH6 Σ Rank tansformations'              °
  127. X 'TEACH7 Σ Partition' 'problems'                                              °
  128. *(1997 6 2 12 24 59 480)                                                       °
  129.  FTEACH1 ÉFX 'TEACH1' 'Σ Introduction to nesting' 'exit ''TEACH1'''            °
  130.   '''In the previous lesson we studied about ARRAYS OF RANK 2. You will'       °
  131.  ''                                                                            °
  132.   '''recall that arrays of rank 2 are tables defined by rows and columns       °
  133.  .'',r'                                                                        °
  134.   '''In this lesson we will study yet another aspect of data, called DEP       °
  135.  TH.'''                                                                        °
  136.   '''before you proceed, you may wish to refresh your memory about array       °
  137.  s''' '''by entering REVIEW.''' 'do'                                           °
  138.   '''We have already hinted at the existence of depth several times. For       °
  139.  '''                                                                           °
  140.   '''example, when discussing reduction, we formed the following express       °
  141.  ion'',r' 'show ''µ/8 5 3'''                                                   °
  142.   '''The result has to be a scalar because reduction reduces rank by one       °
  143.  ,''' '''and 8 5 3 is a vector.'',r'                                           °
  144.   'show ''µµ/8 5 3'' ''µ(µµ/8 5 3)'''                                          °
  145.   '''Yet when displayed, we see a numeric vector of length 8. There woul       °
  146.  d appear'''                                                                   °
  147.   '''to be a contradiction here. The object generated by µ/8 5 3 is inde       °
  148.  ed a'''                                                                       °
  149.   '''rank 0 object. We can use the DISPLAY function to see the structure       °
  150.   of''' '''that result'',r' 'show ''DISPLAY µ/8 5 3'''                         °
  151.   '''The diagram shows two boxes. The outer box is that of a rank 0 arra       °
  152.  y (no'''                                                                      °
  153.   '''arrows), and the inner box is a vector. In APL2 terminology, the ve       °
  154.  ctor'''                                                                       °
  155.   '''has been ENCLOSED. As a result of this enclosure, the object has th       °
  156.  e'''                                                                          °
  157.   '''properties of a "scalar". Its shape is null, and its rank is zero.'       °
  158.  ',r'                                                                          °
  159.   '''The process of placing a box around data is called NESTING, and the       °
  160.  ''' '''results are called NESTED ARRAYS.''' 'do'                              °
  161.   '''The number of boxes that one has to penetrate to reach a simple sca       °
  162.  lar'''                                                                        °
  163.   '''is a property of data called DEPTH. Indeed, APL2 supplies a special       °
  164.  ''' '''primitive monadic function called DEPTH. The symbol is ╧.'''           °
  165.   'do' '''We can test the result of µ compression'',r'                         °
  166.   'show ''╧µ/8 5 3'''                                                          °
  167.   '''Since there are two boxes, depth is clearly 2. Some APL2 arrays may       °
  168.  ''' '''contain data at different depths'',r'                                  °
  169.   'show ''DISPLAY (1 2) 3'''                                                   °
  170.   '''In the above example, (1 2) are at depth 2, while the 3 is at depth       °
  171.   1''' '''We define depth with the following rule:'',r' '0 RULE 20'            °
  172.   '''Given this rule, what would be the depth of a simple scalar?'',r'         °
  173.   'show ''DISPLAY 2'' ''╧2'' ''DISPLAY ''''A'''''' ''╧''''A'''''''             °
  174.   '''Since no box encloses a simple scalar, its depth is clearly ZERO.''       °
  175.  ' '''Furthermore, we have'',r' 'show ''DISPLAY +/∞5'' ''╧+/∞5'''              °
  176.   '''Which gives us the following rule'',r' '0 RULE 21'                        °
  177.   '''Before we discuss depth of APL2 objects any further, it will be use       °
  178.  ful'''                                                                        °
  179.   '''to introduce yet another primitive monadic APL2 function. The symbo       °
  180.  l'''                                                                          °
  181.   '''Γ when used monadically, is called ENCLOSE. It converts the right''       °
  182.  ' '''argument to a rank 0 array, by increasing its depth by one'',r'          °
  183.   'show ''DISPLAY 1 2'' ''DISPLAY Γ1 2'' ''DISPLAY ΓΓ1 2'''                    °
  184.   '''Note that a vector AUTOMATICALLY has a depth of ONE. Also, by rule        °
  185.  21'''                                                                         °
  186.   '''the enclose of a scalar should return that scalar unchanged.'',r'         °
  187. X 'show ''DISPLAY 2'' ''DISPLAY Γ2'' ''DISPLAY ΓΓΓΓΓ2''' 'endd'                °
  188. *(1997 5 25 12 42 17 472)                                                      °
  189.  FTEACH2 ÉFX 'TEACH2;A;B;q' 'Σ Strand notation' 'exit ''TEACH2'''              °
  190.   '''Before we proceed any futher, we must pause, and digress about APL2       °
  191.  '''                                                                           °
  192.   '''SYNTAX. When we first introduced numeric vectors, we merely entered       °
  193.  '''                                                                           °
  194.   '''a sequence of numbers separated by blanks. The number of blanks in'       °
  195.  '' '''fact seems immaterial'',r' 'show ''2 4     6 7'''                       °
  196.   '''Then on another occasion (lesson 4), we introduced an APL2 primitiv       °
  197.  e''' '''function called CATENATE'',r' 'show ''2,4,6,  7'''                    °
  198.   '''As you can see the effect on simple numeric scalars is the same whe       °
  199.  ther'''                                                                       °
  200.   '''we use a blamk or a comma, or both. The comma is indeed a primitive       °
  201.  '''                                                                           °
  202.   '''APL2 function, but the blank is not. It is part of APL2 SYNTAX.'''        °
  203.   'do'                                                                         °
  204.   '''The usage of blanks to separate data is a special technique, called       °
  205.  '''                                                                           °
  206.   '''STRAND notation. This notation can be used with characters as well        °
  207.  as''' '''with blanks. Here is an example'',r'                                 °
  208.   'show ''DISPLAY ''''A'''' ''''B'''' ''''C'''''' ''DISPLAY ''''ABC'''''       °
  209.  ''                                                                            °
  210.   '''The vector ''''ABC'''' was defined using strand notation, as well a       °
  211.  s in the'''                                                                   °
  212.   '''regular way. When we deal with simple scalars, strand notation can        °
  213.  be''' '''thought as equivalent to catenation'',r'                             °
  214.   'show ''DISPLAY ''''A'''',''''B'''',''''C'''''' ''DISPLAY 1 2 3'' ''DI       °
  215.  SPLAY 1,2,3'''                                                                °
  216.   '''When we begin to consider nested arrays, we see a clear distinction       °
  217. X''' '''between strand notation and catenation.''' 'endd'                      °
  218. *(1997 6 2 12 29 8 296)                                                        °
  219.  FTEACH3 ÉFX 'TEACH3;A;B' 'Σ Rank and depth' 'exit ''TEACH3'''                 °
  220.   '''    RANK AND DEPTH'',r'                                                   °
  221.   '''Using strand notation we can create objects of arbitrary depth in''       °
  222.  ' '''a simple way'',r'                                                        °
  223.   'show ''(1 2) (3 4)'' ''DISPLAY (1 2)(3 4)'' ''╧(1 2)(3 4)'''                °
  224.   '''In the above example we used parentheses to generate a vector whose       °
  225.  '''                                                                           °
  226.   '''components are vectors. The resulting object is called a NESTED'''        °
  227.   '''vector. We can also create nested vectors of characters'',r'              °
  228.   'show ''''''Nested'''' ''''vector'''''' ''DISPLAY ''''Nested'''' ''''v       °
  229.  ector'''''''                                                                  °
  230.   '''Nested vector can be a mix of characters and numbers'',r'                 °
  231.   'show ''DISPLAY ''''AGE'''' 24'''                                            °
  232.   '''In this last example, one might ask why do we get a nested vector''       °
  233.  '                                                                             °
  234.   '''instead of a simple vector containing the salars ''''A'''',''''G'''       °
  235.  ',''''E'''',and'''                                                            °
  236.   '''24? (It is nested because ''''AGE'''' IS already a vector.) To get        °
  237.  a'''                                                                          °
  238.   '''depth 1 vector, we would have to catenate the two elements'',r'           °
  239.   'show ''DISPLAY ''''AGE'''',24'''                                            °
  240.   '''Another way to generate a mixed vector without nesting would be'',r       °
  241.  ' 'show ''DISPLAY ''''A'''' ''''G'''' ''''E'''' 24'''                         °
  242.   '''The mere fact that a character STRING is placed in quotes defines i       °
  243.  t'''                                                                          °
  244.   '''to be a vector. This is why parentheses are not required to identif       °
  245.  y''' '''character strings as components of nested vectors.''' 'do'            °
  246.   '''We have shown that a simple vector has a depth of one. What precise       °
  247.  ly''' '''do we mean by the expression "simple vector"?'',r'                   °
  248.   'show ''DISPLAY ∞4'' ''DISPLAY (1 2) 3 4'''                                  °
  249.   '''The first example is a simple vector, because all of its components       °
  250.  '''                                                                           °
  251.   '''are simple scalars. Since we must make a distinction between depth'       °
  252.  '' '''and rank, the question of depth of a vector is not trivial.'''          °
  253.   'do'                                                                         °
  254.   '''Recall that to access an element of a vector, we have to use an'''        °
  255.   '''expression that permits INDEXING of its components. Operationally''       °
  256.  ' '''therefore we can propose the following rule'',r'                         °
  257.   '''   RULE 22. Depth can also be defined as the number of simple steps       °
  258.  '''                                                                           °
  259.   '''            to reach the innermost simple scalar in an array.'',r'        °
  260.   '''This rule is vague so let us see if we can improve on it.''' 'do'         °
  261.   '''Since we can access a simple scalar in a simple array using bracket       °
  262.  ''' '''indexing, simple arrays should also have depth of one'',r'             °
  263.   'show ''DISPLAY 2 2µ''''ABCD'''''' ''DISPLAY 2 3µ∞6'''                       °
  264.   '''One would think that to reach a simple scalar in a nested array, al       °
  265.  l''' '''we need is to apply indexing twice'',r'                               °
  266.   'show ''A╜(1 2) (3 4 5)'' ''DISPLAY A'' ''DISPLAY A[1]'''                    °
  267.   '''But as we can see this does not work! The expression A[1] returns a       °
  268.  '''                                                                           °
  269.   '''rank 0 array, as it SHOULD. The result still has the same depth as        °
  270.  the'''                                                                        °
  271.   '''nested array. We need a way to reach the inner contents of a "non-z       °
  272.  ero''' '''depth rank zero array".''' 'do'                                     °
  273.   '''We could try the indexing function (squad), but it too poduces a ra       °
  274.  nk''' '''zero array'',r' 'show ''DISPLAY 1╙A'''                               °
  275.   '''       THE PICK FUNCTION'',r'                                             °
  276.   '''We define an APL function called PICK. It is a dyadic function usin       °
  277.  g'''                                                                          °
  278.   '''the symbol π. It is SOMEWHAT similar to squad but it differs in tha       °
  279.  t'''                                                                          °
  280.   '''it extracts the contents of nested arrays. Let us see how pick work       °
  281.  s''' '''in a simple case'',r' 'show ''DISPLAY 1πA'' ''DISPLAY 2πA'''          °
  282.   '''So far so good, but let us now add some more depth to A'',r'              °
  283.   'show ''B╜A'' ''B[1]╜ΓA'' ''DISPLAY B'''                                     °
  284.   '''Since B[1] is a rank 0 array, and A is a vector, we had to enclose        °
  285.  A,'''                                                                         °
  286.   '''before we could assign it to B[1]. The vector B has depth 3. To acc       °
  287.  ess'''                                                                        °
  288.   '''the first element of B and obtain the contents of whatever this sca       °
  289.  lar''' '''encloses, we use pick'',r' 'show ''DISPLAY 1πB'' ''µ1πB'''          °
  290.   '''To reach the second element of 1πB we merely write'',r'                   °
  291.   'show ''DISPLAY 1 2πB'''                                                     °
  292.   '''and to reach the third component of that vector'',r'                      °
  293.   'show ''1 2 3πB'''                                                           °
  294.   '''The left argument vector has a special name. It is called the ACCES       °
  295. XS''' '''VECTOR. Now we can rephrase rule 22'',r' '0 RULE 22' 'endd'           °
  296. *(1997 6 2 12 30 19 344)                                                       °
  297.  FTEACH4 ÉFX 'TEACH4;A;B' 'Σ More on depth' 'exit ''TEACH4'''                  °
  298.   '''         USING PICK ON NESTED ARRAYS'',r' 'A╜(1 2)(3 4)'                  °
  299.   'B╜((1 2)(3 4))(3 4)'                                                        °
  300.   '''Rule 22 may still have a flaw. Recall that there is a way to use th       °
  301.  e''' '''indexing function on a scalar'',r' 'show ''(∞0)╙5'''                  °
  302.   '''In a similar way it is possible to pick from a scalar'',r'                °
  303.   'show ''(∞0)π5'''                                                            °
  304.   '''Therefore we could generate an arbitrarily long access vector by'''       °
  305.   '''catenating NULL vectors as rank 0 arrays'',r'                             °
  306.   'show ''DISPLAY B'' ''NULL╜∞0'' ''(1 2 2,NULL,NULL,NULL)πB'''                °
  307.   '''However since the null vector has no components, it has no effect''       °
  308.  ' '''on the length of the access vector.'',r'                                 °
  309.   'show ''DISPLAY 1 2 2,NULL,NULL,NULL'''                                      °
  310.   '''So why do we bother with all this trivia?'''                              °
  311.   '''Because the NULL vector DOES HAVE a function in the access vector!'       °
  312.  '' 'do'                                                                       °
  313.   '''Let us explore this seeming paradox. First we generate C'',r'             °
  314.   'show ''C╜1 2,(Γ3 4) (5 6)'' ''DISPLAY C'''                                  °
  315.   '''The third element of C is a RANK 0 ARRAY containing the vector 3 4.       °
  316.   When''' '''we pick this element'',r' 'show ''DISPLAY 3πC'''                  °
  317.   '''we get the rank 0 array. Now the question is how do we reach the co       °
  318.  mponents'''                                                                   °
  319.   '''of the enclosed vector? The answer, of course is to pick that array       °
  320.   with''' '''a NULL vector'',r'                                                °
  321.   'show ''DISPLAY (3,Γ∞0)πC'' ''(3,(Γ∞0),1)πC'''                               °
  322.   '''The NULL in the acces vector serves the role of a can opener for wh       °
  323.  atever''' '''is inside the rank 0 array.''' 'do'                              °
  324.   '''We can summarize these features in the following rule'',r'                °
  325.   '0 RULE 23'                                                                  °
  326.   '''So far we have been using pick to access elements of nested vectors       °
  327.  .'''                                                                          °
  328.   '''How will pick work with nested arrays of rank greater than 1? Let u       °
  329.  s form''' '''a depth 2 nested array'',r'                                      °
  330.   'show ''A╜2 2µ(1 2) (3 4) 5 6'' ''DISPLAY A'''                               °
  331.   '''This array has depth 2 since the first row components are nested'',       °
  332.  r' 'show ''DISPLAY A[1;2]'''                                                  °
  333.   '''Clearly indexing extracts the component, but retains depth.'',r'          °
  334.   '''We can also try the index function. (The length of the left argumen       °
  335.  t''' '''must be equal to the rank of the array)'',r'                          °
  336.   'show ''DISPLAY 1 2╙A'' ''2 1╙A'''                                           °
  337.   '''Here too, depth is retained on nested elements of the array (to kee       °
  338.  p''' '''them as rank 0 arrays).''' 'do' '''What about using PICK?'''          °
  339.   '''If we were to use pick in the same way as INDEX (1 2πA), it would f       °
  340.  ail,'''                                                                       °
  341.   '''because the first element of the access vector (1) is NOT a valid''       °
  342.  ' '''argument for accessing an element of A (a rank 2 array).'''              °
  343.   '''To reach the second element in the first row, we would have to use        °
  344.  1 2,''' '''but as a rank 0 array.'',r'                                        °
  345.   '''By rule 23 we can reach row 1 column 2 of A by'',r'                       °
  346.   'show ''DISPLAY (Γ1 2)πA'''                                                  °
  347.   '''To reach the second element of the vector in row 1 column 2, we hav       °
  348. Xe''' 'show ''(1 2) 2πA''' 'endd'                                              °
  349. *(1997 8 28 13 7 13 344)                                                       °
  350.  FTEACH5 ÉFX 'TEACH5;A;B' 'Σ Enclose with axis' 'exit ''TEACH5'''              °
  351.   '''       THE ENCLOSE WITH AXIS'',r'                                         °
  352.   '''Monadic enclose need not always produce a rank 0 array. What enclos       °
  353.  e''' '''does is to increase depth. Consider the following array'',r'          °
  354.   'show ''A╜2 2µ∞4'' ''DISPLAY A'' ''DISPLAY ΓA'''                             °
  355.   '''By simply enclosing A, we convert it to a rank 0 array. This is bec       °
  356.  ause we'''                                                                    °
  357.   '''treat A as a single object. However, suppose that we think of A not       °
  358.   as'''                                                                        °
  359.   '''an array, but as a list of two vectors. The first row A[1;] is the        °
  360.  first'''                                                                      °
  361.   '''vector, and the second row A[2;] is the second vector. Can we conve       °
  362.  rt''' '''the array A into a nested list of vectors?'',r'                      °
  363.   'show ''DISPLAY (1 2) (3 4)'''                                               °
  364.   '''Basically, what we want to do is to enclose the rows of A. This can       °
  365.  ''' '''indeed be done'',r' 'show ''DISPLAY Γ[2]A'''                           °
  366.   '''The enclose together with axis specification allows us to form'''         °
  367.   '''equivalent data structures. If we think of array A as a collection'       °
  368.  '' '''of column vectors, we can also enclose columns'',r'                     °
  369.   'show ''DISPLAY Γ[1]A'''                                                     °
  370.   '''Let us see how this concept can be generalized. First we form an ar       °
  371.  ray''' '''of rank 3'',r' 'show ''B╜2 3 4µ∞24'' ''DISPLAY B'''                 °
  372.   '''We could think of B as a single unit, as a vector of arrays, or as        °
  373.  an'''                                                                         °
  374.   '''array of vectors. Each of these data structures is distinctive, but       °
  375.  ''' '''equivalent to B'',r' 'show ''DISPLAY Γ[3]B'''                          °
  376.   '''In this form we see that we can convert B into an array of rank 2 w       °
  377.  ith'''                                                                        °
  378.   '''vector components of length 4. If we think of the first two axes as       °
  379.  ''' '''defining arrays'',r' 'show ''DISPLAY Γ[1 2]B'''                        °
  380.   '''we generate a vector whose components are arrays. By selecting othe       °
  381.  r''' '''axes, we can convert B into other data structures'',r'                °
  382.   'show ''DISPLAY Γ[2 3]B'''                                                   °
  383.   '''When two or more axes are specified, these need not be in increasin       °
  384.  g''' '''sequence'',r' 'show ''DISPLAY Γ[3 2]B'''                              °
  385.   '''The arrays in this last example are the TRANSPOSE of the arrays in'       °
  386.  ''                                                                            °
  387.   '''the previous examples. As a matter of fact, APL2 has a special'''         °
  388.   '''transpose primitive function, φ'',r'                                      °
  389.   'show ''DISPLAY φA'' ''DISPLAY Γ[2 1]A'''                                    °
  390.   '''We will study this function in more detail in a later lesson.'''          °
  391. X 'endd'                                                                       °
  392. *(1997 8 28 13 10 25 404)                                                      °
  393.  FTEACH6 ÉFX 'TEACH6;A;B' 'Σ Disclose of nested arrays'                        °
  394.   'exit ''TEACH6''' '''       RANK TRANSFORMATIONS'',r'                        °
  395.   '''We used the phrase "equivalent data structures". What do we mean'''       °
  396.   '''by that? Such structures are equivalent, because we can readily'''        °
  397.   '''transform one into the other. What can be enclosed, can also be'''        °
  398.   '''disclosed. The monadic disclose gives us that facility'',r'               °
  399.   'show ''DISPLAY π(1 2) (3 4)'''                                              °
  400.   '''Using the disclose, we transformed a depth 2 nested vector (rank 1        °
  401.  array)'''                                                                     °
  402.   '''to a depth 1 rank 2 array. In this example there is no loss of info       °
  403.  rmation,''' '''and the two forms are duals.''' 'do'                           °
  404.   '''The disclose can also be applied with an axis specification. When s       °
  405.  o used,'''                                                                    °
  406.   '''the axis in brackets specifies the placement in the resulting array       °
  407.  .'''                                                                          °
  408.   '''Default is the last axis, namely, a vector is transformed into an a       °
  409.  rray'''                                                                       °
  410.   '''of vectors. Specifying the first axis would transform into an array       °
  411.   of'''                                                                        °
  412.   '''vector components (which is equivalent to the TRANSPOSE of the arra       °
  413.  y).'',r' 'show ''DISPLAY π[2](1 2) (3 4)'''                                   °
  414.   'show ''DISPLAY π[1](1 2) (3 4)'''                                           °
  415.   '''Disclosure of a scalar merely reveals the contents of that scalar''       °
  416.  ,r' 'show ''DISPLAY π5'' ''DISPLAY Γ∞5'' ''DISPLAY πΓ∞5'''                    °
  417.   '''we have the following rules'',r' '0 RULE 24' '0 RULE 25'                  °
  418.   '''The meaning of the last sentence in rule 25 is illustrated in the''       °
  419.  ' '''following examples'',r'                                                  °
  420.   'show ''DISPLAY π(1 2 3) (4 5) 6'' ''DISPLAY π''''ONE'''' ''''PHRASE''       °
  421.  '''''                                                                         °
  422.   '''We will study FILL elements in more detail later, but for now, the'       °
  423.  ''                                                                            °
  424.   '''fill element for a numeric scalar is 0 and for a character scalar i       °
  425.  s''' '''the blank character.''' 'do'                                          °
  426.   '''When we talk of equivalent forms, we talk of homogeneous forms. By'       °
  427.  ''                                                                            °
  428.   '''homogeneous, we mean forms containing as many rank 0 arrays as ther       °
  429.  e''' '''are in the equivalent form of depth 1.'',r'                           °
  430.   '''Can the disclose with axes be applied to more general arrays? Yes,'       °
  431.  '' '''and no. The axis specification must be valid'',r'                       °
  432.   'show ''DISPLAY A╜(2 3µ∞6) (2 3µ''''ONETWO'''')'''                           °
  433.   '''We can treat this as a vector with two arrays, as two arrays in a''       °
  434.  '                                                                             °
  435.   '''vector, or as three four element vectors in an array repeated twice       °
  436.   or'''                                                                        °
  437.   '''in many other ways. Here are some examples of valid transformations       °
  438.  '',r' 'show ''DISPLAY πA'' ''DISPLAY π[1 2]A'' ''DISPLAY π[2 3]A'''           °
  439.   '''Consider now the following nested array'',r'                              °
  440.   'show ''DISPLAY B╜(2 2µ∞4) (5 6)'''                                          °
  441.   '''The components of this array have different ranks. Therefore neithe       °
  442.  r'''                                                                          °
  443.   '''π[1] nor π[1 2] will work as you will find out if you try. The'''         °
  444.   '''expression π[1]B will not work because the first component of B'''        °
  445.   '''is an array, and requires an axis specification of the form [1 2]''       °
  446.  '                                                                             °
  447.   '''or [2 1]. The expression π[1 2] will also not work, because the'''        °
  448.   '''second component of B is a vector. It requires an axis specificatio       °
  449.  n''' '''of the form π[1].''' 'do'                                             °
  450.   '''We have therefore the following rules'',r' '0 RULE 26' '0 RULE 27'        °
  451.   '''By rule 26, rank 0 arrays are allowed. Here are some examples'',r'        °
  452.   'show ''B[2]╜5'' ''DISPLAY πB'' ''B[2]╜ΓΓ(5 6)'' ''DISPLAY B'' ''DISPL       °
  453.  AY πB'''                                                                      °
  454.   '''How about ππB? Would this be a valid transformation, and if so what       °
  455. X''' '''would be its shape?''' 'endd'                                          °
  456. *(1997 6 2 12 34 53 496)                                                       °
  457.  FTEACH7 ÉFX 'TEACH7' 'Σ Dyadic enclose or partition' 'exit ''TEACH7'''        °
  458.   '''      PARTITION'',r'                                                      °
  459.   '''We have studied the monadic disclose with and without axis specific       °
  460.  ation'''                                                                      °
  461.   '''and the monadic enclose with and without axis specification. We als       °
  462.  o'''                                                                          °
  463.   '''studied the dyadic form of the disclose. It was called pick. What''       °
  464.  '                                                                             °
  465.   '''about a dyadic enclose. There is such a primitive function, and it'       °
  466.  ''                                                                            °
  467.   '''is called PARTITION. Let us see what it looks like, and how it work       °
  468.  s.''' 'do'                                                                    °
  469.   '''Suppose we have a character vector that has been entered at the'''        °
  470.   '''terminal'',r'                                                             °
  471.   'show ''PHRASE╜''''Once upon a time there was an elf.'''''''                 °
  472.   '''If we are to process text, it might be useful to break up a string'       °
  473.  ''                                                                            °
  474.   '''into its component words. In APL2 this can readily be done with the       °
  475.  ''' '''partition function'',r'                                                °
  476.   'show ''DISPLAY (PHRASE⌠'''' '''')ΓPHRASE'''                                 °
  477.   '''Before we investigate precisely how this works, notice that the las       °
  478.  t'''                                                                          °
  479.   '''period is included in the word ''''elf.''''. Let us remove this per       °
  480.  iod.'',r' 'show ''DISPLAY (~PHRASEε'''' .'''')ΓPHRASE'''                      °
  481.   '''And since we do not want to lose any punctuation, we should also'''       °
  482.   '''enter'',r' 'show ''DISPLAY (PHRASEε'''' .'''')ΓPHRASE'''                  °
  483.   '''These two expressions produce nested arrays that are equivalent to        °
  484.  the'''                                                                        °
  485.   '''original phrase. (We will see how to reconstruct the original phras       °
  486.  e''' '''from these two nested vectors in the next lesson.)''' 'do'            °
  487.   '''Right now, let us see how partition works. The expression'',r'            °
  488.   'show ''PHRASE⌠'''' '''''''                                                  °
  489.   '''returns a vector. The fact that this vector is boolean is irrelevan       °
  490.  t'',r' 'show ''DISPLAY (5⌡PHRASE⌠'''' '''')ΓPHRASE'''                         °
  491.   '''What does matter is the presence of zeros, AND the position in the'       °
  492.  ''                                                                            °
  493.   '''vector where the value increases. Let us call the left argument the       °
  494.  ''' '''partition vector. We have the following rules'',r' '0 RULE 28'         °
  495.   '0 RULE 29'                                                                  °
  496.   '''Let us see how this works. For illustration, we will use numeric'''       °
  497.   '''vectors'',r' 'show ''DISPLAY (∞6)Γ∞6'''                                   °
  498.   '''since the partition vector increases at every step, the result is''       °
  499.  ' '''a partition into six component vectors.''' 'do'                          °
  500.   '''In the next example, the partition vector never increases'''              °
  501.   'show ''DISPLAY (6µ5)Γ∞6'' ''DISPLAY 1Γ∞6'''                                 °
  502.   '''Since a scalar is always replicated to match the shape of the other       °
  503.  ''' '''argument, it too generates no partition.''' 'do'                       °
  504.   '''Now let us see how we can partition the vector in other ways'',r'         °
  505.   'show ''2|∞6'' ''DISPLAY (2|∞6)Γ∞6'' ''1+2|∞6'' ''DISPLAY (1+2|∞6)Γ∞6'       °
  506.  ''                                                                            °
  507.   '''In the first example the even numbers have been removed because'''        °
  508.   '''their residue (modulo 2) is zero. In the second example, the'''           °
  509.   '''partition is the same, but the even numbers are included because'''       °
  510. X '''the partition vector does not contain any zeros.''' 'endd'                °
  511. *(1997 7 13 12 28 49 504)                                                      °
  512.  Faddquote ÉFX 'u╜addquote w'                                                  °
  513.   'Σ Put quotes around a string, and double existing quotes'                   °
  514. X 'u╜ÉAV[40],((1+w=ÉAV[40])/w),ÉAV[40]'                                        °
  515. *(1997 7 24 13 20 38 476)                                                      °
  516.  Fav ÉFX 'av;A;N;I;ÉIO' 'Σ Display characters in ÉAV' 'ÉIO╜0'                  °
  517.   'A╜22 78µ'' ''' 'N╜3 0«φ12 22µ1+∞356' 'A[;,(6⌡∞12)°.+2 3 4]╜N'               °
  518.   'A[;6+6⌡∞12]╜φ12 22µÉAV' 'ΣA[8 10 13;6]╜'' ''' 'A[13;6]╜'' '''               °
  519. X 'A[14+∞8;68 69 70 72]╜'' ''' 'A'                                             °
  520. *(1991 11 11 8 25 13 316)                                                      °
  521.  Fdate ÉFX 'u╜date w' 'Σ Format date and time of day' 'u╜«6╞w'                 °
  522. X 'u╜('' ''⌠u)Γu' 'u╜εu,■''-- .. '''                                           °
  523. XCdig 1 10 1234567890                                                          °
  524. *(1997 9 9 13 0 45 372)                                                        °
  525.  Fdisclaimer ÉFX 'disclaimer' 'Σ Copyright statement'                          °
  526.   '(10µ'' ''),''Copyright, Z. V. Jizba, 1995,1996,1997'',r'                    °
  527.   ''' This and subsequent workspaces labelled TEACHxx are made available       °
  528.  '''                                                                           °
  529.   '''at no cost to anyone who desires to learn how to use effectively'''       °
  530.   '''the IBM/OS2 version of APL2.'',r'                                         °
  531.   '''This software is provided "AS IS" with no WARRANTY of any kind, eit       °
  532.  her'''                                                                        °
  533.   '''express or implied. Any risk in its use resides with you, the user        °
  534.  of''' '''these tutorials.'',r' '''     ACKNOWLEDGEMENTS'',r'                  °
  535.   ''' In writing these tutorials, I am greatly indebted to Roy Sykes, wh       °
  536.  ose'''                                                                        °
  537.   '''excellent lectures increased my understanding of the language.'''         °
  538.   '''Discussions with the late Harry Bertucelli clarified a number of'''       °
  539.   '''concepts and caused me to change some improper terminology that was       °
  540.  '''                                                                           °
  541.   '''used in previous versions of these tutorials. Mr. Benjamin Archer''       °
  542.  '                                                                             °
  543.   '''kindly checked out a nearly final version, bringing to my attention       °
  544.  ''' '''some ommisions, misspellings, and invalid terminology.'',r'            °
  545. X '''(PRESS ENTER to continue)'''                                              °
  546. *(1997 7 13 12 28 50 508)                                                      °
  547.  Fdo ÉFX 'do;T;E' 'Σ Expression driver' 'E╜''''' 'æ╜''      ''' 'T╜æ'          °
  548.   '╕(^/'' ''=T)/0'                                                             °
  549.   '╕(('':''εT)doif ''B╜evaldd (+/^\'''' ''''=T)╟T'')/2'                        °
  550.   '''E╜ÉEM'' ÉEA T' '╕(0=µ,E)/2'                                               °
  551.   '''This is not a valid APL2 expression''' 'æ╜''*''' '╕(''?''⌠╞1╟æ)/2'        °
  552. X 'E' '╕2'                                                                     °
  553. *(1997 7 13 12 28 50 508)                                                      °
  554.  Fdoif ÉFX 'U╢╜V╢ doif W╢;t╢' 'Σ Rule' '╕(^/~U╢╜V╢)/0'                         °
  555. X '''U╢╜V╢ doif■ W╢'' ÉEA ''»V╢/W╢'''                                          °
  556. *(1997 9 9 12 50 14 444)                                                       °
  557.  Fendd ÉFX 'endd' 'Σ end of special feature' '20µ''²'' ╪ ╕(4<µÉLC)/0'          °
  558. X 'do'                                                                         °
  559. *(1997 8 15 11 54 26 524)                                                      °
  560.  Ferase ÉFX 'erase;t;EXIT;GO;HELP;DISPLAY;REVIEW;RULE;DISCLAIMER'              °
  561.   'Σ Erase all global functions and variables' 't╜ÉNL 3'                       °
  562.   't╜(~t^.εlc,'' '')≡t' 't╜ÉEX(~t[;∞5]^.=''TEACH'')≡t' 't╜ÉNL 2'               °
  563. X 't╜ÉEX(~t^.εlc,'' '')≡t'                                                     °
  564. *(1997 7 27 13 47 41 608)                                                      °
  565.  Fevaldd ÉFX 'u╜evaldd w;c;n' 'Σ Evaluate direct definition' 'u╜0'             °
  566.   'n╜(w∞''Σ'')-1' 'c╜(((n╞w)⌠'':'')Γn╞w),Γ''ΣDD '',(n+1)╟w'                    °
  567.   '╕((1 label╞c)doif ''''''Invalid label'''''')/0'                             °
  568.   '╕((2=µc)doif ''u╜showdd 1╙c'')/0'                                           °
  569.   '╕((3=ÉNC╞c)doif ''u╜⌡µÉ╜(╞c),'''' is already defined.'''''')/0'             °
  570.   '╕((3=µc)doif ''u╜simdd c'')/0' 'c╜(Γ''α∙ aw'')replace■c'                    °
  571.   'u╜ε''u╜'',((''a''εεc[2 3 4])/''a ''),(╞c),'' w;t;b'''                       °
  572.   'u╜u(5πc)(''b╜(t╜'',(3πc),'')/'',addquote ''u╜'',4πc)'                       °
  573. X 'u╜u,''╕(t doif b)/0''(''u╜'',2πc)' 'u╜╧ÉFX u'                               °
  574. *(1997 7 25 13 27 52 564)                                                      °
  575.  Fexit ÉFX 'V exit W;T' 'Σ Exit if too many suspended functions'               °
  576.   '╕(0⌠ÉNC ''V'')/L0 ╪ V╜10' 'L0:╕(V>µÉLC)/0'                                  °
  577.   '''There are too many suspended functions''' '''Please enter '',W'           °
  578. X '╕'                                                                          °
  579. *(1997 7 26 12 33 39 536)                                                      °
  580.  Fget ÉFX 'U╜V get W;t;T;ÉPR' 'Σ Prompt for response from keyboard'            °
  581.   'ÉPR╜T╜ÉAV[ÉIO+255] ╪ ╕(0⌠ÉNC ''V'')/L0 ╪ V╜1' 'L0:V╜V╧1' 'æ╜W ╪ t╜æ'        °
  582.   'U╜(+/^\t=T)╟t' '╕(''╕''⌠╞U)/L1 ╪ ╕' 'L1:╕V/0' 't╜(U⌠'' '')ΓU'               °
  583. X 'U╜(µt),(ΓU),t'                                                              °
  584. *(1997 7 28 13 33 8 424)                                                       °
  585.  Fglobals ÉFX 'globals' 'Σ Initialize useful global variables'                 °
  586.   'uc╜''ABCDEFGHIJKLMNOPQRSTUVWXYZ'''                                          °
  587.   'lc╜''abcdefghijklmnopqrstuvwxyz''' 'dig╜''1234567890'''                     °
  588. X 'r╜ÉAV[13+ÉIO]' 'q╜'''''''''                                                 °
  589. *(1997 7 3 12 47 6 368)                                                        °
  590.  Finitialize ÉFX 'initialize;T' 'Σ Initialize workspace'                       °
  591.   '''AT ALL TIMES, TO CONTINUE, PRESS RETURN!'',r'                             °
  592.   '''To see disclaimers enter:'',r,''    disclaimer''' 'do' 'erase'            °
  593.   'globals'                                                                    °
  594.   '''Make sure the CAP LOCK light on your keyboard (upper right) is ON!'       °
  595. X'' 'endd'                                                                     °
  596. *(1997 7 27 13 14 33 444)                                                      °
  597.  Flabel ÉFX 'u╜v label w'                                                      °
  598.   'Σ Return 1 if label w does not begin with a cap'                            °
  599.   '╕(0⌠ÉNC ''v'')/L0 ╪ v╜0' 'L0:v╜v╧1 ╪ w╜εw ╪ ╕v/L1 ╪ ╕(u╜0⌠ÉNC w)/0'         °
  600. X 'L1:╕(u╜~^/wεlc,uc,dig)/0' 'u╜w[1]εlc,dig'                                   °
  601. XClc 1 26 abcdefghijklmnopqrstuvwxyz                                           °
  602. *(1997 7 13 12 28 55 528)                                                      °
  603.  Fnon ÉFX 'non;T;RC;ET;R' 'Σ Ignore keyboard entry' 'æ╜''      ''' 'T╜æ'       °
  604.   '╕(0=µ(T⌠'' '')/T)/0' '(RC ET R)╜ÉEC T' '╕(0=RC)/2'                          °
  605. X '╕((1=RC)doif ''R'')/2' '╕2'                                                 °
  606. *(1997 7 13 12 28 55 528)                                                      °
  607.  Fnotb ÉFX 'u╜notb w' 'Σ Remove trailing blanks'                               °
  608.   '╕((1<╧w)doif ''u╜notb■ w'')/0' '╕((1<µµw)doif ''u╜πnotb Γ[2]w'')/0'         °
  609. X 'u╜(1-(,'' ''⌠Φw)∞1)╟w'                                                      °
  610. *(1997 7 27 12 55 6 496)                                                       °
  611.  Fproblems ÉFX 'problems' 'Σ Problems'                                         °
  612.   '''That is all for this lesson. Remember, if you want to practice,'''        °
  613.   '''and plan to use direct definitions, be sure to first enter GO.'''         °
  614.   '''Direct definitions will then be accepted. To exit GO, enter EXIT.''       °
  615.  ,r'                                                                           °
  616.   '''To erase a previously defined DIRECT DEFINITION FUNCTION, enter'',r       °
  617.  ' '''       )ERASE functionname'',r'                                          °
  618. X '''WARNING! do not use )ERASE on other labels.'',r'                          °
  619. XCq 0 '                                                                        °
  620. XCr 0                                                                         °
  621. *(1997 7 13 12 28 56 532)                                                      °
  622.  Freplace ÉFX 'u╜v replace u;i;r;s' 'Σ Replace elements in v in u'             °
  623.   'i╜Γ∞µu' 's╜2πv╜(v⌠'' '')Γv' 'i╜⌡r╜i⌡■Γ[1]u°.=╞v'                            °
  624. X 'u[(εi)/εr]╜s[(εi)/εi⌡■∞µs]'                                                 °
  625. *(1997 7 13 12 28 56 532)                                                      °
  626.  Fround ÉFX 'U╜V round W' 'Σ Half adjust to V th decimal'                      °
  627. X 'U╜(╛0.5+W⌡10*V)÷10*V'                                                       °
  628. *(1997 6 2 12 45 31 452)                                                       °
  629.  Frule ÉFX 'V rule W' 'Σ Additional remarks on rules'                          °
  630.   '╕(2=ÉNC ''V'')/L ╪ V╜0' 'L:╕(~(╞W)ε∞8)/LA ╪ V rulea╞W ╪ ╕L0'                °
  631.   'LA:╕(~(╞W)ε9 10 11)/LB ╪ V ruleb╞W ╪ ╕L0'                                   °
  632.   'LB:╕(~(╞W)ε11+∞8)/LC ╪ V rulec╞W ╪ ╕L0'                                     °
  633.   'LC:╕((╞W)=19+∞10)/L20,L21,L22,L23,L24,L25,L26,L27,L28,L29'                  °
  634.   '''This rule number is undefined'' ╪ ╕' 'L20:╕(V>1)/L20X'                    °
  635.   '''   RULE 20. The depth of an APL array is defined as the maximum'''        °
  636.   '''            depth to reach a simple scalar.'',2µr' '╕(0=V)/L0'            °
  637.   'L20X:''The concept of depth provides the means of building complex da       °
  638.  ta'''                                                                         °
  639.   '''structures. This is the first of several rules dealing with depth.'       °
  640.  '' '╕L0' 'L21:╕(V>1)/L21X'                                                    °
  641.   '''   RULE 21. No APL2 expression can generate a depth 1 object contai       °
  642.  ning'''                                                                       °
  643.   '''            ONLY a scalar. (Or, a scalar can not have depth 1.)'',2       °
  644.  µr' '╕(0=V)/L0'                                                               °
  645.   'L21X:''This rule can be treated as an axiom of APL2. In theory it is'       °
  646.  ''                                                                            °
  647.   '''possible to enclose a scalar, but that would lead to a significantl       °
  648.  y'''                                                                          °
  649.   '''different language. EXAMPLE: The reduction of a vector must return'       °
  650.  ''                                                                            °
  651.   '''a scalar. Doing repeated reduction could produce enclosed scalars''       °
  652.  '                                                                             °
  653.   '''that would require removal of such depths before allowing compariso       °
  654.  ns.''' '╕L0' 'L22:╕(V>1)/L22X'                                                °
  655.   '''   RULE 22. The depth of an array is given by the maximum length of       °
  656.  '''                                                                           °
  657.   '''            the access vector associated with that array.'',2µr'          °
  658.   '╕(0=V)/L0'                                                                  °
  659.   'L22X:''When you use function DISPLAY, depth can be obtained by counti       °
  660.  ng'''                                                                         °
  661.   '''the number of boxes you must cross from outside to reach the innerm       °
  662.  ost''' '''scalar.''' '╕L0' 'L23:╕(V>1)/L23X'                                  °
  663.   '''   RULE 23. Null elements of an access vector must be included to''       °
  664.  ' '''            match corresponding rank zero arrays.'',2µr'                 °
  665.   '╕(0=V)/L0'                                                                  °
  666.   'L23X:''The access vector works from left to right. The leftmost eleme       °
  667.  nt'''                                                                         °
  668.   '''removes the outermost box, and the rightmost element removes the'''       °
  669.   '''innermost box.''' '╕L0' 'L24:╕(V>1)/L24X'                                 °
  670.   '''   RULE 24. The monadic disclose of a rank 0 array reveals the cont       °
  671.  ents''' '''            of that array.'',2µr' '╕(0=V)/L0'                      °
  672.   'L24X:''This is a consequence of rule 21. A simple scalar is immune to       °
  673.  ''' '''to the process of enclosure or disclosure.''' '╕L0'                    °
  674.   'L25:╕(V>1)/L25X'                                                            °
  675.   '''   RULE 25. The monadic disclose (without axis specification) reduc       °
  676.  es'''                                                                         °
  677.   '''            depth by one, by adding a new last axis to the disclose       °
  678.  d'''                                                                          °
  679.   '''            elements. FILL elements are used to complete the array.       °
  680.  '',2µr' '╕(0=V)/L0'                                                           °
  681.   'L25X:''The fill element for numberic arrays is zero. The fill element       °
  682.   for'''                                                                       °
  683.   '''character arrays is a blank. The fill element for mixed arrays in''       °
  684.  ' '''APL2 will be discussed in later sessions.''' '╕L0'                       °
  685.   'L26:╕(V>1)/L26X'                                                            °
  686.   '''   RULE 26. Simple disclose requires that all components of a neste       °
  687.  d'''                                                                          °
  688.   '''            array have the same rank (or be rank 0 arrays).'',2µr'        °
  689.   '╕(0=V)/L0'                                                                  °
  690.   'L26X:''We use the phrase ''''simple disclose'''' because, as we will        °
  691.  learn'''                                                                      °
  692.   '''later, you can define your own rules on how to disclose nested'''         °
  693.   '''arrays.''' '╕L0' 'L27:╕(V>1)/L27X'                                        °
  694.   '''   RULE 27. Forms can be equivalent only if all rank 0 arrays conta       °
  695.  in'''                                                                         °
  696.   '''            the same rank and shape (or rank 0 arrays)--recursively       °
  697.  --'',2µr' '╕(0=V)/L0'                                                         °
  698.   'L27X:''Earlier we discussed functions that are dual. These are one to       °
  699.  '''                                                                           °
  700.   '''one transformations of data. Equivalent forms can be thought of as'       °
  701.  '' '''a kind of dual representation of data structures.''' '╕L0'              °
  702.   'L28:╕(V>1)/L28X'                                                            °
  703.   '''   RULE 28. A zero in a partition vector will remove the associated       °
  704.  ''' '''            element.'',2µr' '╕(0=V)/L0'                                °
  705.   'L28X:''The components of a partition vector must be positive integers       °
  706.   or'''                                                                        °
  707.   '''zero. The presence of a zero is treated much the same as in boolean       °
  708.  ''' '''reduction.''' '╕L0' 'L29:╕(V>1)/L29X'                                  °
  709.   '''   RULE 29. Each place in the partition vector, where the value'''        °
  710.   '''            INCREASES marks a partition.'',2µr' '╕(0=V)/L0'               °
  711.   'L29X:''A partition must ocurr BETWEEN two consecutive elements. These       °
  712.  ''' '''need not be simple scalars'',r'                                        °
  713.   'show ''DISPLAY (1 1 2 2)Γ(1 2) ''''AB'''' (3 4) ''''C''''''' 'L0:do'        °
  714. X '╕(0=µW╜1╟W)/0' '╕L'                                                         °
  715. *(1997 6 1 12 42 53 524)                                                       °
  716.  Frulea ÉFX 'V rulea W' 'Σ Additional remarks on rules'                        °
  717.   '╕(2=ÉNC ''V'')/L ╪ V╜0' 'L:╕(W=∞8)/L1,L2,L3,L4,L5,L6,L7,L8'                 °
  718.   '''For rules not described in this lesson, see workspace INDEX'''            °
  719.   '╕0' 'L1:╕(V>1)/L1X'                                                         °
  720.   '''RULE 1. For arithmetic operations a vector must be matched with a''       °
  721.  ' '''        scalar, or another vector of the same length.'',2µr'             °
  722.   '╕(0=V)/0'                                                                   °
  723.   'L1X:''This is the first of many rules dealing with the concept of'''        °
  724.   '''CONFORMABILITY. This means that elements to be processed by APL2'''       °
  725.   '''must in some ways match.''' '╕0' 'L2:╕(V>1)/L1X'                          °
  726.   '''RULE 2. The reduction operator places the function, (specified to''       °
  727.  '                                                                             °
  728.   '''        its left), between all pairs of the vector. It then evaluat       °
  729.  es''' '''        the resulting expression.'',2µr' '╕(0=V)/0'                  °
  730.   'L2X:''There is more to this, but at this stage the rule is essentiall       °
  731.  y''' '''correct.''' '╕0' 'L3:╕(V>1)/L1X'                                      °
  732.   '''RULE 3. All APL2 expressions are evaluated from right to left'',2µr       °
  733.  ' '╕(0=V)/0'                                                                  °
  734.   'L3X:''This rule is valid in APL, which is the earlier version of APL2       °
  735.  .'''                                                                          °
  736.   '''For APL2, the rule has been restated in a totally different way.'''       °
  737.   '''However until we introduce some concepts that are new to APL2, the'       °
  738.  '' '''newer form reduces to RULE 3 as stated.''' '╕0' 'L4:╕(V>1)/L1X'         °
  739.   '''RULE 4. Vectors in APL2 can have lengths of zero and one.'',2µr'          °
  740.   '╕(0=V)/0'                                                                   °
  741.   'L4X:''This is the first concept that begins to introduce the idea tha       °
  742.  t no'''                                                                       °
  743.   '''matter what the APL expression, if it is valid, it must produce a''       °
  744.  '                                                                             °
  745.   '''valid APL2 object. (Shades of which came first, the chicken or the        °
  746.  egg)''' '╕0' 'L5:╕(V>1)/L1X'                                                  °
  747.   '''RULE 5. The shape of a variable is a vector. The shape of a vector'       °
  748.  '' '''        is a vector of length 1. The shape of a scalar is a'''          °
  749.   '''        vector of length zero.'',2µr' '╕(0=V)/0'                          °
  750.   'L5X:''The left argument of RESHAPE is by definition a vector. If APL2       °
  751.   is'''                                                                        °
  752.   '''to be internally consistent, the inverse operation to RESHAPE (whic       °
  753.  h''' '''is indeed SHAPE) MUST return the same type of object.''' '╕0'         °
  754.   'L6:╕(V>1)/L1X'                                                              °
  755.   '''RULE 6. Reduction of a scalar returns that scalar REGARDLESS of'''        °
  756.   '''        the function to the left of the reduction operator.'',2µr'        °
  757.   '╕(0=V)/0'                                                                   °
  758.   'L6X:''Reduction BY DEFINITION reduces the RANK by one. The exception        °
  759.  is'''                                                                         °
  760.   '''for objects whose rank is zero. Since there is no such thing as'''        °
  761.   '''negative rank, reduction is undefined for scalars, and therefore'''       °
  762.   '''the result is to leave the APL2 object unchanged.''' '╕0'                 °
  763.   'L7:╕(V>1)/L1X'                                                              °
  764.   '''RULE 7. The reduction of a null vector produces the identity'''           °
  765.   '''        element of the function.'',2µr' '╕(0=V)/0'                        °
  766.   'L7X:''A null vector may be empty, but it still has rank equal to one.       °
  767.  '''                                                                           °
  768.   '''Therefore its reduction MUST be a scalar. Since there is no data'''       °
  769.   '''in a null vector, the scalar must be such that it has no effect'''        °
  770.   '''on the operation performed by the function: Zero added to anything'       °
  771.  ''                                                                            °
  772.   '''has no effect in addition; one multiplied by anything has no effect       °
  773.  ''' '''in multiplication; and so on.''' '╕0' 'L8:╕(V>1)/L1X'                  °
  774.   '''RULE 8. Parentheses can (and should) be used to modify the right'''       °
  775.   '''        to left rule to improve efficiency'',2µr' '╕(0=V)/0'              °
  776.   'L8X:''Parentheses are used in APL2 for other purposes as well, but fo       °
  777.  r'''                                                                          °
  778.   '''now, we will use them just to modify the right to left rule (RULE 3       °
  779.  ).'''                                                                         °
  780.   '''If you place parentheses where they are not needed, APL2 will ignor       °
  781. Xe''' '''them.'''                                                              °
  782. *(1997 6 1 12 49 37 488)                                                       °
  783.  Fruleb ÉFX 'V ruleb W' 'Σ Additional remarks on rules'                        °
  784.   '╕(2=ÉNC ''V'')/L ╪ V╜0' 'L:╕(9 10 11=╞W)/L9,L10,L11 ╪ ╕0'                   °
  785.   'L9:╕(V>1)/L9X'                                                              °
  786.   '''   RULE 9: To produce a quote inside a character string, enter'''         °
  787.   '''           the quote twice.'',2µr' '╕(0=V)/0'                             °
  788.   'L9X:''This is used in most other programming languages.''' '╕0'             °
  789.   'L10:╕(V>1)/L10X'                                                            °
  790.   ''' RULE 10: In replication the left argument must contain only'''           °
  791.   '''          integers. The length of the left argument must be'''            °
  792.   '''          the same as the length of the right argument.'',2µr'            °
  793.   '╕(0=V)/0'                                                                   °
  794.   'L10X:''The left argument may also have negative integers. Only the nu       °
  795.  mber'''                                                                       °
  796.   '''of positive integers and zeros must add up to the length of the rig       °
  797.  ht'''                                                                         °
  798.   '''argument. Replicate with negative integers is not discussed here be       °
  799.  cause'''                                                                      °
  800.   '''the explanation requires concepts that have not yet been mentioned.       °
  801.  '',r' 'show ''1 1 2 ²1 0 1 1 1 ²1 1 0 1 1/''''CALIFORNIAN''''''' '╕0'         °
  802.   'L11:╕(V>1)/L11X'                                                            °
  803.   '''   RULE 11: The expression inside square brackets must return'''          °
  804.   '''            positive integers in the range of the length of'''            °
  805.   '''            the indexed vector. If the expression is MISSING,'''          °
  806.   '''            the result is as if the brackets were missing.'',2µr'         °
  807.   '╕(0=V)/0'                                                                   °
  808.   'L11X:''The expression in brackets need not be a vector. It could be a       °
  809.  n''' '''array as in the next example'',r'                                     °
  810. X 'show ''''''CALIFORNIA''''[3 3µ?9µ10]'' ''(∞4)[3 3µ∞4]'''                    °
  811. *(1997 6 2 12 11 17 260)                                                       °
  812.  Frulec ÉFX 'V rulec W' 'Σ Additional remarks on rules'                        °
  813.   '╕(2=ÉNC ''V'')/L ╪ V╜0'                                                     °
  814.   'L:╕((11+∞8)=╞W)/L12,L13,L14,L15,L16,L17,L18,L19' 'L12:╕(V>1)/L12X'          °
  815.   '''   RULE 12. A scalar function is one that will produce a result on'       °
  816.  ''                                                                            °
  817.   '''            every scalar element of a vector or array. EXAMPLES:'''       °
  818.   '''            + - ⌡ ÷ * are numeric scalar functions. , µ ∞ are NOT''       °
  819.  ' '''            scalar functions.'',r' '╕(0=V)/0'                            °
  820.   'L12X:''This distinction will become clearer when we study more genera       °
  821.  lized''' '''arrays.''' '╕0' 'L13:╕(V>1)/L13X'                                 °
  822.   '''   RULE 13. Scalars that work with vectors also work with arrays.''       °
  823.  ,r' '╕(0=V)/0'                                                                °
  824.   'L13X:''No problem with simple scalars, but this rule may need modific       °
  825.  ation'''                                                                      °
  826.   '''when we deal with "rank zero arrays" that are not simple scalars.''       °
  827.  ' '╕0' 'L14:╕(V>1)/L14X'                                                      °
  828.   '''   RULE 14. Arrays with the same shape can be processed with scalar       °
  829.  ''' '''functions.'',r' '╕(0=V)/0'                                             °
  830.   'L14X:''Arrays with distinct shapes can be processed as we shall see,        °
  831.  but'''                                                                        °
  832.   '''there will be restrictions, and we will use new operators.''' '╕0'        °
  833.   'L15:╕(V>1)/L15X'                                                            °
  834.   '''   RULE 15. The number of semicolons inside brackets must equal the       °
  835.  ''' '''            rank of the array MINUS 1.'',r' '╕(0=V)/0'                 °
  836.   'L15X:''There is no further generalization to this rule.''' '╕0'             °
  837.   'L16:╕(V>1)/L16X'                                                            °
  838.   '''   RULE 16. An axis specification can be a scalar, a vector or a'''       °
  839.   '''            null vector.'',r' '╕(0=V)/0'                                  °
  840.   'L16X:''Recall that null array is the ONLY bracket specification that'       °
  841.  ''                                                                            °
  842.   '''makes sense for a scalar. We will use that property when dealing wi       °
  843.  th''' '''rank 0 arrays.''' '╕0' 'L17:╕(V>1)/L17X'                             °
  844.   '''   RULE 17. An axis specification can be any array containing'''          °
  845.   '''            valid integers.'',r' '╕(0=V)/0'                               °
  846.   'L17X:''With this rule, you can generate an APL2 object of arbitrary''       °
  847.  ' '''rank.''' '╕0' 'L18:╕(V>1)/L18X'                                          °
  848.   '''   RULE 18. The shape of the result of axis specification is the'''       °
  849.   '''            catenation of the shapes of expressions inside brackets       °
  850.  .'''                                                                          °
  851.   '''            If the expression for an axis is missing, the shape for       °
  852.  ''' '''            that axis is unchanged.'',r' '╕(0=V)/0'                    °
  853.   'L18X:''This is merely the rule for implementation of rule 17.'''            °
  854.   '╕0' 'L19:╕(V>1)/L19X'                                                       °
  855.   '''   RULE 19. Simple reduction of arrays removes its last dimension.'       °
  856. X',r' '╕(0=V)/0' 'L19X:''This is a generalization of rule 2.''' '╕0'           °
  857. *(1997 7 13 12 28 57 536)                                                      °
  858.  Fshow ÉFX '╢V show ╢W;╢T;╢B' 'Σ Display and execute ╢W'                       °
  859.   '╢T╜(0=ÉNC ''╢V'')doif ''╢V╜0'''                                             °
  860.   '╕((0=╧╢W)doif ''show ╢W,'''' '''''')/0'                                     °
  861.   '╕((1<╧╢W)doif ''╢V show ■╢W'')/0' '''      '',╢W'                           °
  862. X '╕((╢V^'':''ε╢W)doif ''╢T╜evaldd ╢W'')/L0' '''ÉEM'' ÉEA ╢W' 'L0:do'          °
  863. *(1997 7 13 12 28 57 536)                                                      °
  864.  Fshowdd ÉFX 'u╜showdd w;a;b;c;r'                                              °
  865.   'Σ Display a direct definition function'                                     °
  866.   '╕((1=╧w)doif ''u╜showdd Γw'')/u╜0'                                          °
  867.   '╕((3⌠ÉNC╞w)doif ''(ε╞w),'''' is not a function'''''')/0'                    °
  868.   'c╜Γ[2]ÉCR╞w' 'c╜notb(2╞c),(Γ''aw α∙'')replace■2╟c'                          °
  869.   '╕((~''ΣDD''╧3╞2πc)doif ''''''Not a direct definition function'''''')/       °
  870.  0' 'u╜1' 'b╜('' ''⌠╞c)Γ╞c' 'a╜''      ''' 'r╜2╟3πc'                           °
  871.   '╕((3=µc)doif ''a,(╞w),'''':'''',r,(3<µ2πc)/''''   Σ'''',3╟2πc'')/0'         °
  872.   'a╜a,(╞w),'':'',(2╟5πc),'':''' 'b╜(+\r=''('')-+\r='')''' 'b╜b∞0'             °
  873. X 'a╜a,(²3╟(b-1)╞3╟r),'':'',2╟»(b+2)╟r' 'a,(3<µ2πc)/''  Σ'',3╟2πc'             °
  874. *(1997 7 13 12 28 57 536)                                                      °
  875.  Fshowfn ÉFX 'U╜V showfn W;F;N;T;ÉIO' 'Σ Simulate ╖W[É]'                       °
  876.   'T╜(0=ÉNC ''V'')doif ''V╜0''' 'ÉIO╜0'                                        °
  877.   'U╜r,''      '',''╖'',W,''[É]'',(╞V)╞''╖''' 'N╜1╞µF╜ÉCR W' 'N╜«∞N'           °
  878.   'N╜(N⌠'' '')ΓN' 'F╜(π''['',■N,■Γ''] ''),F'                                   °
  879.   'T╜(1<µ,V)doif ''F╜F[1╟V;]'' ''U╜''''''''''' 'U╜²1╟U,r,,F,r'                 °
  880. X 'U╜((-+/^\'' ''=ΦU)╟U),('' ╖'')[╞V],r'                                       °
  881. *(1997 7 13 12 28 58 540)                                                      °
  882.  Fsimdd ÉFX 'u╜simdd w;e' 'Σ Direct definition mode' 'u╜0'                     °
  883.   '╕((0⌠ÉNC╞w)doif ''''''Already defined'''''')/0' 'e╜''α''ε2πw'               °
  884.   'w[2]╜Γ''u╜'',''α∙ aw'' replace 2πw' 'w╜w[1 3 2]'                            °
  885. X 'w[1]╜Γε''u╜'',(e/''a ''),w[1],'' w''' 'u╜╧ÉFX w'                            °
  886. *(1992 6 3 9 59 17 424)                                                        °
  887.  Ftab ÉFX 'U╜V tab W;T;A;B;C;D;E;F;G;M;ÉPW' 'Σ Tabulate list W'                °
  888.   'T╜(0=ÉNC ''V'')doif ''V╜0''' 'M╜''Invalid data for tabulation'''            °
  889.   'V╜4╞V' 'ÉPW╜130╛30⌐G╜V[2]+79⌡V[2]=0'                                        °
  890.   'L1:╕((1<╧W)doif ''''''W╜∞0'''' ÉEA ''''W╜πW'''''')/L1'                      °
  891.   '╕(((0=µεW)δ2<µµW)doif ''U╜(~V╧4╞0)/M'')/0'                                  °
  892.   'T╜(1≥µµU╜«W)doif ''U╜πW╜(U⌠'''' '''')ΓU'''                                  °
  893.   'T╜(0<V[1])doif ''U╜(«(Φ1,╞µW)µ(V[3]µ'''' ''''),∞(╞µW)-V[3]),'''' ''''       °
  894.  ,U''' '╕(G<30)/0' 'T╜(F╜µεV[4])+C╜1╟B╜µA╜(V[3],0)╟U'                          °
  895.   'T╜⌐(1╞B)÷1⌐╛(ÉPW+F)÷T' 'U╜(E╜(V[3],C)╞U),[1](B╜T,1╟B)╞A'                    °
  896.   '''D╜εV[4]'' ÉEA ''D╜ÉAV[εV[4]+33⌡V[4]=0]''' 'L0:A╜(T,0)╟A'                  °
  897. X '╕(0=1╞µA)/0' 'U╜U,(((T+V[3]),µD)µD),E,[1]B╞A' '╕L0'                         °
  898. *(1997 7 13 12 28 59 544)                                                      °
  899.  Ftest ÉFX 'U╜V test W;P'                                                      °
  900.   'Σ Describe problem in W, (correct answer in V)' 'U╜2' 'L1:W'                °
  901.   'É╜''      ''' 'P╜æ' '''╕L0'' ÉEA ''P╜»P''' '╕(V╧P)/0'                       °
  902. X 'L0:╕(0=U╜U-1)/0' '''Incorrect. Try again''' '╕L1'                           °
  903. XCuc 1 26 ABCDEFGHIJKLMNOPQRSTUVWXYZ                                           °
  904. *(1996 4 6 16 3 10 220)                                                        °
  905.  Fvec ÉFX 'U╜vec' 'Σ Draw 2d vector in plane'                                  °
  906.   'U╜r,r,''2-        +'',r '':      °'',r,''1-    °'',r'                       °
  907. X 'U╜U,'' :  °'',r,''0|....|....|'',r,'' 0    1    2'',r'                      °
  908.