home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / teach.zip / TEACH11A.ATF < prev    next >
Text File  |  1997-09-19  |  72KB  |  885 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. *(1996 4 6 16 4 50 384)                                                        °
  8.  FBIND ÉFX 'BIND' 'Σ Binding rules of APL2'                                    °
  9.   '''  1. Brackets bind to the left'''                                         °
  10.   '''  2. Left arrow binds to the left'''                                      °
  11.   '''  3. Dyadic operator binds to the right'''                                °
  12.   '''  4. Strand notation binding'''                                           °
  13.   '''  5. Operator binds to its left'''                                        °
  14.   '''  6. Function binds to its left'''                                        °
  15.   '''  7. Function binds to its right'''                                       °
  16. X '''  8. Left arrow binds to its right'',r'                                   °
  17. *(1997 9 14 12 30 44 504)                                                      °
  18. XFDISCLAIMER ÉFX 'DISCLAIMER' 'Σ Copyright statement' 'disclaimer'             °
  19. *(1996 4 6 16 5 21 272)                                                        °
  20.  FDISPLAY ÉFX 'D╜S DISPLAY A;ÉIO;R;C;HL;HC;HT;HB;VL;VB;V;W;N;B'                °
  21.   'Σ DISPLAY A GENERAL ARRAY IN PICTORIAL FORM'                                °
  22.   'Σ  NORMAL CALL IS MONADIC.  DYADIC CALL USED ONLY IN'                       °
  23.   'Σ    RECURSION TO SPECIFY DISPLAY RANK, SHAPE, AND DEPTH.' 'ÉIO╜0'          °
  24.   '»(0=ÉNC ''S'')/''S╜µA''' 'R╜╞µ,S                   Σ PSEUDO RANK.'          °
  25.   'C╜''┐┌└┘''                 Σ UR, UL, LL, AND LR CORNERS.'                   °
  26.   'HL╜''─''                   Σ HORIZONTAL LINE.'                              °
  27.   'HC╜HL,''Θ╕'',HL,''~+ε''      Σ HORIZONTAL BORDERS.'                         °
  28.   'HT╜HC[(0<R)⌡1+0<╞²1╞,S]' 'ΣW╜,0╧■╞0µΓ(1⌐µA)╞A'                              °
  29.   'HB╜HC[3+3╛(''2⌡~A╧«A'' ÉEA ''1+╞ε0⌡(1⌐⌡/µA)╞,A'')+3⌡1<µµS]'                 °
  30.   'VL╜''│''                   Σ VERTICAL LINE.'                                °
  31.   'VB╜VL,''Φ╟''               Σ VERTICAL BORDER.'                              °
  32.   'V╜VB[(1<R)⌡1+0<²1╞²1╟,S]'                                                   °
  33.   '»(0εµA)/''A╜(1⌐µA)µΓ╞A''   Σ SHOW PROTOTYPE OF EMPTIES.'                    °
  34.   '╕(1<╧A)/GEN' '╕(2<µµA)/D3'                                                  °
  35.   'D╜«A                     Σ SIMPLE ARRAYS.' 'W╜1╞µD╜(²2╞1 1,µD)µD'           °
  36.   'N╜²1+1╟µD' '╕(0=µµA)/SS'                                                    °
  37.   'D╜(C[1],V,((W-1)µVL),C[2]),((HT,NµHL),[0]D,[0]HB,NµHL),C[0],(WµVL),C[       °
  38.  3]' '╕0' 'SS:HB╜((0 '' '')=╞0µΓA)/'' -'''                                     °
  39.   'D╜'' '',('' '',[0]D,[0]HB,Nµ'' ''),'' ''' '╕0'                              °
  40.   'GEN:D╜«DISPLAY■A          Σ ENCLOSED ...' 'N╜Dδ.⌠'' '''                     °
  41.   'D╜(Nδ~1ΦN)≡D' 'D╜(δ≡~''  ''╤D)/D' 'D╜((1,µS)µS)DISPLAY D'                   °
  42.   '╕(2≥µ,S)╟D3E,0' 'D3:D╜0 ²1╟0 1╟«ΓA         Σ MULT-DIMENSIONAL ...'          °
  43.   'W╜1╞µD' 'N╜²1+1╟µD'                                                         °
  44.   'D╜(C[1],V,((W-1)µVL),C[2]),((HT,NµHL),[0]D,[0]HB,NµHL),C[0],(WµVL),C[       °
  45.  3]' 'D3E:N╜²2+µ,S'                                                            °
  46. X 'V╜C[Nµ1],[0]VB[1+0<²2╟,S],[0](((²3+╞µD),N)µVL),[0]C[Nµ2]' 'D╜V,D'           °
  47. *(1996 4 6 16 5 21 272)                                                        °
  48.  FEXIT ÉFX 'EXIT' 'Σ Exit from function' '''To log off type:   )OFF'''         °
  49. X '╕'                                                                          °
  50. *(1996 4 6 16 5 21 272)                                                        °
  51.  FGO ÉFX 'GO;T;E;B' 'Σ Expression driver' 'L0:B╜E╜''''' 'æ╜''      '''         °
  52.   'T╜æ' '╕(^/'' ''=T)/L0' '╕((^/'')OFF ''=5╞6╟T)doif ''EXIT'')/0'              °
  53.   '╕(('':''εT)doif ''B╜evaldd (+/^\'''' ''''=T)╟T'')/L0'                       °
  54.   '''E╜ÉEM'' ÉEA T' '╕(0=µ,E)/L0' '╕B/L0'                                      °
  55.   '''This is not a valid APL2 expression''' 'æ╜''*'''                          °
  56. X '╕(''?''⌠╞1╟æ)/L0' 'E' '╕L0'                                                 °
  57. *(1997 8 22 12 51 22 528)                                                      °
  58.  FHELP ÉFX 'HELP;N;I;T' 'Σ Help to student' '''WHAT TO DO'',r'                 °
  59.   '''  ° To get out of the lesson''' '''       ENTER: EXIT'''                  °
  60.   '''  ° To log off APL2'''                                                    °
  61.   '''       FIRST, ENTER: EXIT  THEN ENTER: )OFF'''                            °
  62.   '''  ° To get help''' '''       ENTER: HELP'''                               °
  63.   '''  ° When you see the prompt on a blank line'''                            °
  64.   '''       ENTER AN APL2 EXPRESSION - OR JUST PRESS: ENTER''' 'do'            °
  65.   '''  ° If you get this line'',r'                                             °
  66.   '''This is not a valid APL2 expression'',r,''*'',r'                          °
  67.   '''        YOU CAN EITHER''' '''           A. PRESS: ENTER'''                °
  68.   '''           B. PRESS: ? and then ENTER to see what was incorrect'''        °
  69.   '''              in your expression causing that response'',r' 'do'          °
  70.   '''HINTS'',r'                                                                °
  71.   '''This lesson is made up of '',(«N╜9),'' components named TEACHx, whe       °
  72.  re''' '''the x stands for a digit:'',r' 'I╜0'                                 °
  73.   'L0:T╜''TEACH'',(«I╜I+1)' 'T,'' '',1╟notb(ÉCR T)[2;]' '╕(N>I)/L0'            °
  74.   'do'                                                                         °
  75.   '''You may review either of these components separately. To do that,''       °
  76.  '                                                                             °
  77.   '''first enter EXIT (and RETURN), then enter the name of the lesson.''       °
  78.  ' '''component (e.g. TEACH4).'',r'                                            °
  79.   '''To re-start the lesson, just enter TEACH'',r'                             °
  80.   '''When the screen fills up, it is a good idea to move the cursor to''       °
  81.  ' '''the start of a convenient paragraph, and press ENTER'',r'                °
  82.   '''You may also wish to press PAGE UP to review the prevous pages.'''        °
  83.   'do' '''RESOURCES'',r'                                                       °
  84.   '''You may also enter ONE of the following words at a time'',r'              °
  85. X '''REVIEW   To review the previous lesson''' 'endd'                          °
  86. *(1996 4 6 16 5 21 272)                                                        °
  87.  FREVIEW ÉFX 'REVIEW' 'Σ Review of inner product'                              °
  88.   '''The inner product operator (also known as the DOT product) applies'       °
  89.  ''                                                                            °
  90.   '''two functions to a dyadic argument. The operator requires a left'''       °
  91.   '''and a right function, as well as a left and a right argument.'',r'        °
  92.   '''Although it will produce a result when the arguments are scalar'''        °
  93.   '''or vectors, inner product is desidgned primarily to be used with'''       °
  94.   '''arrays of rank 2 or greater.''' 'do'                                      °
  95.   'show ''5+.⌡3'' ''1 2 3+.⌡3 2 1'' ''(2 3µ∞6)+.⌡3 2µ∞6'''                     °
  96.   '''In the first example above, only the left function is applied to'''       °
  97.   '''the two scalars. The last two examples illustrate the principal'''        °
  98.   '''characteristic of the inner product. Just as the reduction operator       °
  99.  '''                                                                           °
  100.   '''reduces one dimension, the inner product reduces two dimensions,'''       °
  101.   '''namely the trailing one fron the left argument, and the leading one       °
  102.  ''' '''from the right argument.'',r'                                          °
  103.   '''Although many primitive functions can be used in the inner product,       °
  104.  ''' '''the most common ones are ^.= and +.⌡'',r'                              °
  105.   'show ''(π''''ONE'''' ''''TWO'''' ''''SIX'''')^.=φπ''''SIX'''' ''''ONE       °
  106.  ''''''' 'show ''(3 3µ∞9)+.⌡3 2µΦ∞6'''                                         °
  107.   '''The first example illustrates the usage to find matching rows and''       °
  108.  '                                                                             °
  109.   '''columns. The second example illustrates matrix multiplication. We''       °
  110.  '                                                                             °
  111.   '''will study this numeric method in more detail in subsequent lessons       °
  112.  .''' 'do'                                                                     °
  113.   '''The following examples illustrate some more unconventional usage'''       °
  114.   '''of the inner product operator'',r'                                        °
  115.   'show ''(3 2µ(1 2) (3 4) (5 6))δ.ε2 2µ(1 2) (3 4)'' ''(∞4)+.,4 2µ∞8'''       °
  116. X 'endd'                                                                       °
  117. *(1997 7 4 12 17 58 460)                                                       °
  118.  FTEACH ÉFX 'TEACH' 'Σ Start lesson 11: APL2 by Zdenek V JIZBA'                °
  119.   'exit ''TEACH''' 'initialize' 'TEACH1 Σ User defined functions'              °
  120.   'TEACH2 Σ Using QUAD FIX and Canonical representation'                       °
  121.   'TEACH3 Σ Defining function ROMAN' 'TEACH4 Σ The line editor'                °
  122.   'TEACH5 Σ Editorial on programming styles'                                   °
  123.   'TEACH6 Σ Re-entering function definition'                                   °
  124.   'TEACH7 Σ The session manager editor' 'TEACH8 Σ Headers'                     °
  125. X 'TEACH9 Σ Local and global objects' 'problems'                               °
  126. *(1997 6 17 12 44 35 524)                                                      °
  127.  FTEACH1 ÉFX 'TEACH1' 'Σ Introduction to defined functions'                    °
  128.   'exit ''TEACH1'''                                                            °
  129.   '''In the previous lesson we learned a lot about an APL2 operator'''         °
  130.   '''called the ARRAY PRODUCT. There is more to that operator, but'''          °
  131.   '''we will postpone that, and take a look in this lesson at user'''          °
  132.   '''defined functions. To review the previous lesson, enter REVIEW.'',r       °
  133.  '                                                                             °
  134.   '''Before you continue, you may also wish to review lesson 5 first.'''       °
  135.   '''  [T0 do that, enter EXIT to leave this lesson and then )LOAD TEACH       °
  136.  5]''' 'do'                                                                    °
  137.   '''In lessons 3 4 and 5 we learned about DIRECT DEFINITION, but we als       °
  138.  o'''                                                                          °
  139.   '''found that this way of defining new APL2 functions is not supported       °
  140.   by'''                                                                        °
  141.   '''APL2. In this lesson we will study three ways to define a function.       °
  142.  '''                                                                           °
  143.   '''   (APL2 is available on mainframes as well as on the PC. Mainframe       °
  144.  '''                                                                           °
  145.   '''    versions support editors not currently available on the PC. Her       °
  146.  e''' '''    we describe only the techniques available on the PC.)'''          °
  147.   'do'                                                                         °
  148.   '''One way to define an APL2 function is to use the DEL MODE (╖). A se       °
  149.  cond way'''                                                                   °
  150.   '''is to apply a SYSTEM FUNCTION called QUAD FIX (ÉFX). A third way is       °
  151.   to use''' '''the APL2 editor.''' 'do'                                        °
  152.   '''The ╖ mode form invokes a special APL2 editor that prompts for'''         °
  153.   '''expressions to be enterd at the keyboard. While in the ╖ mode,'''         °
  154.   '''it is not possible to evaluate APL2 expressions. Conversely, during       °
  155.  '''                                                                           °
  156.   '''execution of APL2 expressions, it is not possible to switch to the'       °
  157.  ''                                                                            °
  158.   '''╖ mode. It is because of this, that you will not be able to test'''       °
  159.   '''╖ function editing until after the completion of this lesson.'',r'        °
  160.   '''The second way to define new APL2 functions is with the ÉFX System'       °
  161.  ''                                                                            °
  162.   '''function. This approach allows you to specify the function as a'''        °
  163.   '''character array first. The ÉFX function then converts the character       °
  164.  ''' '''array to a user defined function.'',r'                                 °
  165.   '''In the third approach, you can access the EDIT pull down menu and''       °
  166.  '                                                                             °
  167.   '''select the "Open Object" option. (If the function you wish to defin       °
  168.  e'''                                                                          °
  169.   '''already exists, there is a faster method: place the cursor on the''       °
  170.  ' '''name of the function and click twice).'''                                °
  171.   '''We will describe these techniques in more detail in this lesson. We       °
  172.   will'''                                                                      °
  173.   '''also introduce the supporting SYSTEM FUNCTIONS ÉCR, ÉEX and ÉNC.'''       °
  174. X 'endd'                                                                       °
  175. *(1997 6 16 12 48 15 456)                                                      °
  176.  FTEACH2 ÉFX 'TEACH2;R;T;FN;AVERAGE' 'Σ Quad fix' 'exit ''TEACH2'''            °
  177.   '''Suppose we desire to define function AVERAGE. The objective of this       °
  178.  '''                                                                           °
  179.   '''function is to add the elements of an array by reducing its last'''       °
  180.   '''dimension, and dividing the sum by the number of elements added'',r       °
  181.  ' 'show ''(+/∞5)÷5'' ''(+/2 3µ∞6)÷3'''                                        °
  182.   '''The function AVERAGE is monadic, and will produce a result. Therefo       °
  183.  re'''                                                                         °
  184.   '''the first thing we have to define is this structural information'',       °
  185.  r' 'show ''FN╜''''U╜AVERAGE R'''''''                                          °
  186.   '''We have generated a character string named FN. The contents of FN i       °
  187.  s'''                                                                          °
  188.   '''called the HEADER. It describes to APL2 the fact that we are about        °
  189.  to'''                                                                         °
  190.   '''define a function called AVERAGE, which is monadic, and returns a v       °
  191.  alue.'''                                                                      °
  192.   '''Next we enter a comment to describe the nature of this function.'',       °
  193.  r' 'show ''FN╜FN ''''Σ Average of last dimension'''''''                       °
  194.   '''A comment like this is not required, but it is an excellent practic       °
  195.  e''' '''and is highly recommended'',r' 'show ''FN╜FN,Γ''''U╜+/R'''''''        °
  196.   '''Can you see why we had to use the idiom ,Γ to catenate the string''       °
  197.  '                                                                             °
  198.   '''''''U╜+/R'''' to the nested vector FN? (If you have problems with t       °
  199.  his,'''                                                                       °
  200.   '''try the expressions DISPLAY FN string, DISPLAY FN,string and'''           °
  201.   '''DISPLAY FN,Γstring.)'',r' 'show ''DISPLAY FN'''                           °
  202.   '''This first expression generates U by reducing R. To find the averag       °
  203.  e,''' '''we divide U by the scalar 0¥µR'',r'                                  °
  204.   'show ''FN╜FN,Γ''''U╜U÷0¥µR'''''''                                           °
  205.   '''Why the expression 0¥µR? This insures that the array +/R is divided       °
  206.  '''                                                                           °
  207.   '''by a scalar no matter what its rank. (Look at lesson 5 to see why t       °
  208.  he'''                                                                         °
  209.   '''idiom 0¥ returns the last element of a vector as a scalar.)'''            °
  210.   'do' 'show ''FN'' ''DISPLAY πFN'''                                           °
  211.   '''Either of the two versions of FN, a vector of simple character vect       °
  212.  ors'''                                                                        °
  213.   '''or a simple character array, can be then used as a right argument t       °
  214.  o''' '''the APL2 System function ÉFX'',r'                                     °
  215.   'show ''ÉFX FN'' ''AVERAGE ∞5'' ''R╜AVERAGE 2 3µ∞6'' ''R'''                  °
  216.   '''To see how the defined function AVERAGE looks, there is another Sys       °
  217.  tem'''                                                                        °
  218.   '''Function called ÉCR (the CR stands for CANONICAL REPRESENTATION).''       °
  219.  ,r' 'show ''ÉCR ''''AVERAGE'''''''                                            °
  220.   '''Why did we separate the expression (+/R)÷0¥µR into two expressions?       °
  221.  '''                                                                           °
  222.   '''Actually a combined expression would have worked fine, but we do'''       °
  223.   '''want to make a point. In a defined function, the evaluation proceed       °
  224.  s'''                                                                          °
  225.   '''from top to bottom, much the same as if we had written the individu       °
  226.  al'''                                                                         °
  227.   '''expressions one after the other. So when we enter AVERAGE ∞5, the''       °
  228.  '                                                                             °
  229.   '''function first assigns to R the result of evaluation of ∞5. Then it       °
  230.  '''                                                                           °
  231.   '''finds the comment line which it ignores. The next line U╜+/R is'''        °
  232.   '''evaluated, and then U╜U÷0¥µR completes the algorithm. The final val       °
  233.  ue'''                                                                         °
  234.   '''of U is then returned. If this value is assigned to a variable as i       °
  235.  n'''                                                                          °
  236.   '''the second example, the assignment is made in the usual manner.'''        °
  237.   'do'                                                                         °
  238.   '''What would happen if we were to try fixing a function with invalid'       °
  239.  '' '''arguments?'',r' 'show ''ÉFX ''''U╜5TEST'''' ''''U╜1'''''''              °
  240.   '''In the example we have tried to fix a function named 5TEST. Names i       °
  241.  n'''                                                                          °
  242.   '''APL2 are not allowed beginning with digits. The fixing process fail       °
  243.  ed,'''                                                                        °
  244.   '''and the return is an integer (actually the 1 means that the error''       °
  245.  ' '''was detected in the first character vector.)''' 'do'                     °
  246.   '''The ÉFX system function also accepts a four element boolean left'''       °
  247.   '''argument. The values control execution and the ability to display t       °
  248.  he''' '''defined function.''' 'do'                                            °
  249.   '''It is not always possible to solve problems as in function AVERAGE.       °
  250.   In'''                                                                        °
  251.   '''some algorithm it may be necessary to make decisions, and to procee       °
  252.  d'''                                                                          °
  253.   '''according to such decisions. This means that we need a way to skip'       °
  254.  ''                                                                            °
  255.   '''certain expressions (essentially a GO TO branch). To see how APL2''       °
  256. X' '''handles this problem, we will define another function.''' 'endd'         °
  257. *(1997 6 17 12 52 3 428)                                                       °
  258.  FTEACH3 ÉFX 'TEACH3;R;T;ROMAN;RO;RX;FN;AVERAGE'                               °
  259.   'Σ Branching and labels' 'exit ''TEACH3'''                                   °
  260.   '''In lesson 5 we solved the problem of converting an Arabic number'''       °
  261.   '''to Roman notation. We defined function ROMAN, and two additional'''       °
  262.   '''functions, RO and RX, to be used as subroutines.'',r'                     °
  263.   '1 show ''ROMAN:RO 10 10 10 10ÿ∙:∙>3999:''''This number is too large''       °
  264.  '''''                                                                         °
  265.   'T╜''RO:(∙[1]µ''''M''''),(''''CDM''''RX ∙[2]),(''''XLC''''RX ∙[3]),'''       °
  266.  'IVX''''RX ∙[4]''' '1 show T'                                                 °
  267.   '1 show ''RX:((∙>4)µα[2]),(5|∙)µα[1]:4=5|∙:α[1,2+∙=9]'''                     °
  268.   'show ''ROMAN 27'''                                                          °
  269.   '''Function ROMAN calls on function RO, and this function in turn call       °
  270.  s on'''                                                                       °
  271.   '''function RX. This is a reasonable way to solve problems with APL2.        °
  272.  Let''' '''us now define this function using the ÉFX approach.'',r'            °
  273.   '''As before, we begin with the header'',r'                                  °
  274.   'show ''FN╜''''U╜ROMAN ARABIC'''''''                                         °
  275.   '''Note that we labelled the argument to ROMAN ''''ARABIC''''. This is       °
  276.  '''                                                                           °
  277.   '''perfectly valid. APL2 will understand that U and ARABIC are variabl       °
  278.  es.''' '''We can now continue defining function ROMAN.'',r'                   °
  279.   'show ''FN╜FN ''''Σ To convert Arabic number to Roman notation'''''''        °
  280.   '''As before we begin with a comment. Now let us consider the algorith       °
  281.  m.'''                                                                         °
  282.   '''Right away we see that we have a problem. We need to decide whether       °
  283.  '''                                                                           °
  284.   '''the value of the right argument to ROMAN is greater than 3999. If i       °
  285.  t is,''' '''then we merely return the string'',r'                             °
  286.   '''       ''''This number is too large'''''',r'                              °
  287.   '''If ARABIC is less than 4000, we apply expression RO 10 10 10 10ÿARA       °
  288.  BIC''' 'do' 'show ''FN╜FN,Γ''''╕(ARABIC<4000)/L0'''''''                       °
  289.   '''The right arrow (╕) is a symbol understood by the portion of APL2''       °
  290.  '                                                                             °
  291.   '''that evaluates user defined functions. It has no meaning within'''        °
  292.   '''stand alone APL2 expressions.''' 'do'                                     °
  293.   '''Within user defined functions, it tells the APL2 INTERPRETER to'''        °
  294.   '''ignore the normal sequence of expressions, but instead to find'''         °
  295.   '''the expression that begins with the string ''''L0''''.'',r'               °
  296.   '''This branch will take place only if the expression to the right of'       °
  297.  '' '''the ╕ returns a value other than null.''' 'do'                          °
  298.   'show ''FN╜FN,Γ''''U╜''''''''This number is too large.'''''''''''''''        °
  299.   '''If ARABIC is greater than 3999, then the expression ARABIC<4000 wil       °
  300.  l'''                                                                          °
  301.   '''return a zero, and the reduction 0/L0 will produce a null vector.''       °
  302.  '                                                                             °
  303.   '''The APL2 interpreter ignores any branch to a null vector. Therefore       °
  304.  '''                                                                           °
  305.   '''the next expression to be evaluated will be the assignment of the''       °
  306.  ' '''string ''''This number is too large'''' to U''' 'do'                     °
  307.   'show ''FN╜FN,Γ''''╕0'''''''                                                 °
  308.   '''A branch to zero is interpreted by APL2 as a command to EXIT the'''       °
  309.   '''user defined function.''' 'do'                                            °
  310.   'show ''FN╜FN,Γ''''L0:U╜RO 10 10 10 10ÿARABIC'''''''                         °
  311.   '''This expression is LABELLED. The label L0 is recognized by the'''         °
  312.   '''presence of the colon (:). If the value of ARABIC is less than 4000       °
  313.  '''                                                                           °
  314.   '''this expression will be evaluated, and the function will return the       °
  315.  ''' '''value of U.''' 'do'                                                    °
  316.   '''We now have completed the programming part. Let us take a look at''       °
  317.  ' '''the array FN that we have just generated'',r' 'show ''πFN'''             °
  318.   '''Before we can apply ÉFX, we could make sure that there is no APL2''       °
  319.  '                                                                             °
  320.   '''object with that name. There is the System Function ÉNC for that'''       °
  321.   '''purpose'',r' 'show ''ÉNC ''''ROMAN'''''' ''ÉNC ''''FN'''''''              °
  322.   '''System Function ÉNC is called NAME CLASSIFICATION. It tells whether       °
  323.  '''                                                                           °
  324.   '''the right argument exists as an APL2 object. The 3 for ROMAN means'       °
  325.  ''                                                                            °
  326.   '''that ROMAN already exists as a function. The 2 for FN means that'''       °
  327.   '''there is a variable called FN'',r' 'show ''ÉEX ''''ROMAN'''''''           °
  328.   '''System function ÉEX (for EXPUNGE) has been applied to erase functio       °
  329.  n'''                                                                          °
  330.   '''ROMAN. The value 1 returned by ÉEX acknowledges successful erasure.       °
  331.  '',r'                                                                         °
  332.   '''It is good programming practice to apply ÉNC to a label, and then t       °
  333.  o'''                                                                          °
  334.   '''use ÉEX just before using ÉFX. (In APL2 ÉFX will override a previou       °
  335.  sly''' '''defined function with the same name.)'',r'                          °
  336.   'show ''ÉNC ''''ROMAN'''''''                                                 °
  337.   '''The 0 returned by ÉNC ''''ROMAN'''' means that the label ROMAN is n       °
  338.  ot in''' '''use.'',r'                                                         °
  339.   'show ''ÉFX FN'' ''ÉNC ''''ROMAN'''''' '' ROMAN 27 '''                       °
  340.   '''Function ROMAN has been re-defined with FN, and works properly.'''        °
  341.   'do' '''     DIAMOND SEPARATOR'',r'                                          °
  342.   '''APL2 supports a notation called the "diamond separator". The symbol       °
  343.   ╪'''                                                                         °
  344.   '''allows entering two or more expressions to be placed on a single li       °
  345.  ne.''' '''The expressions are evaluated from left to right'',r'               °
  346.   'show ''''''First line'''' ╪ ∞5 ╪ ''''Third line'''''''                      °
  347.   '''Using the diamond, we can re-define function ROMAN'',r'                   °
  348.   'FN╜''U╜ROMAN ARABIC'''                                                      °
  349.   'FN╜''U╜ROMAN ARABIC'' ''Σ To convert Arabic number to Roman notation'       °
  350.  ''                                                                            °
  351.   'FN╜FN,Γ''╕(ARABIC<4000)/L0 ╪ U╜''''This number is too large.'''' ╪ ╕0       °
  352.  ''' 'FN╜πFN,Γ''L0:U╜RO 10 10 10 10ÿARABIC''' 'show ''FN'' ''ÉFX FN'''         °
  353.   '''We have collapsed three lines into one. This technique is more than       °
  354.  '''                                                                           °
  355.   '''a device to reduce the number of APL lines in a function definition       °
  356.  .'''                                                                          °
  357.   '''As we will see later, it can be used to provide an important elemen       °
  358.  t'''                                                                          °
  359.   '''of structural programming. But now let us verify that the new versi       °
  360. Xon''' '''of ROMAN works properly'',r' 'show ''ROMAN 1997''' 'endd'            °
  361. *(1997 8 30 11 35 44 580)                                                      °
  362.  FTEACH4 ÉFX 'TEACH4;R;T;FN;MOVAVG' 'Σ Del mode' 'exit ''TEACH4'''             °
  363.   '''Let us now describe the process of defining functions with the ╖'''       °
  364.   '''symbol.'',r'                                                              °
  365.   '''WARNING! You will not be able to try out the examples on your own''       °
  366.  '                                                                             °
  367.   '''         until AFTER the lesson is completed. You may, however,'''        °
  368.   '''         use ÉFX to try out ideas of your own.''' 'do'                    °
  369.   '''We will try to define function MOVAVG. This function will evaluate'       °
  370.  '' '''the moving average of a numeric vector or array. Here are two'''        °
  371.   '''examples of the algorithm to be defined'',r'                              °
  372.   'show ''(3+/∞6)÷3'' ''(2+/2 4µ∞8)÷2'''                                       °
  373.   '''We begin with the header. There are two arguments to this function:       °
  374.  '''                                                                           °
  375.   '''One is the data to be averaged, and the other is the averaging leng       °
  376.  th.'''                                                                        °
  377.   '''The function therefore should be dyadic. What we have to decide is'       °
  378.  ''                                                                            °
  379.   '''which should be the left argument, and which the right argument.'''       °
  380.   'do'                                                                         °
  381.   '''There are no hard and fast rules for deciding where arguments shoul       °
  382.  d'''                                                                          °
  383.   '''be placed. On the basis of past experience, and for other practical       °
  384.  '''                                                                           °
  385.   '''reasons, it is a good idea to place the data to be modified as the'       °
  386.  '' '''right argument'',r' '''      ╖U╜L MOVAVG DATA''' '''[1]'''              °
  387.   'FN╜''U╜L MOVAVG DATA'' ''Σ Moving average of length L on data'''            °
  388.   'FN╜FN,''U╜L+/DATA'' ''U╜U÷L''' 'T╜ÉFX FN' 'do'                              °
  389.   '''The symbol ╖ preceeding the header expression is a toggle switch.''       °
  390.  '                                                                             °
  391.   '''It transfers control to the APL2 function editor. At this point'''        °
  392.   '''anything you enter at the terminal becomes part of the function'''        °
  393.   '''under definition. You can always tell when you are in the APL2'''         °
  394.   '''edit mode, because you are prompted by a line count'',r'                  °
  395.   '''[1] Σ Moving average of length L on DATA''' '''[2]''' 'do'                °
  396.   '''Pressing ENTER terminates the line entry. A new prompt is issued'''       °
  397.   '''for the next line'',r' '''[2] U╜L+/DATA''' '''[3] U╜U÷L╖''' 'do'          °
  398.   '''Entering the symbol ╖ at the end of line 3 ends the function'''           °
  399.   '''definition mode, as well as the process of definimg MOVAVG'',r'           °
  400. X 'show ''3 MOVAVG ∞6''' 'endd'                                                °
  401. *(1997 8 30 11 44 26 544)                                                      °
  402.  FTEACH5 ÉFX 'TEACH5;FN;T;MOVAVG' 'Σ Programming style'                        °
  403.   'exit ''TEACH5'''                                                            °
  404.   'FN╜''U╜L MOVAVG DATA'' ''Σ Running average of length L on data'''           °
  405.   'FN╜FN,''U╜L+/DATA'' ''U╜U÷L''' 'T╜ÉFX FN' '''PROGRAMMING STYLES'',r'        °
  406.   '''Before proceeding with further description of defined functions,'''       °
  407.   '''it is apropriate at this point to make a few EDITORIAL remarks abou       °
  408.  t'''                                                                          °
  409.   '''programming style. Since programming is a highly personal effort'''       °
  410.   '''there is always room for disagreement. Hence, in this and in later'       °
  411.  ''                                                                            °
  412.   '''sessions, my personal comments will be preceeded by the heading'''        °
  413.   'do' '''''''PROGRAMMING STYLES''''.''' 'do'                                  °
  414.   '''First, it should be clear that function MOVAVG has been designed'''       °
  415.   '''merely to illustrate concepts in function definition. In practice''       °
  416.  '                                                                             °
  417.   '''it does not make sense to write a special function, where a simple'       °
  418.  ''                                                                            °
  419.   '''line of code, namely (L+/DATA)÷L will suffice. One should therefore       °
  420.  ''' '''ask, why and when should one define an APL2 function.'',r'             °
  421.   '''In my opinion there are three fundamental reasons for APL2 user def       °
  422.  ined''' '''functions:'',r,r,''  1. General utility functions'''               °
  423.   '''  2. Problem solving functions'',r,''  3. Subroutines'',r'                °
  424.   '''Utility APL2 functions are those that one might want to use in many       °
  425.  '''                                                                           °
  426.   '''different applications. Problem solving functions are those that'''       °
  427.   '''solve specific problems. An example of these would be function ROMA       °
  428.  N.'''                                                                         °
  429.   '''ROMAN uses functions RO and RX as subroutines. Defining RO and RX''       °
  430.  '                                                                             °
  431.   '''as separate APL2 functions simplifies the code, and makes it easier       °
  432.  ''' '''to understand and to maintain.''' 'do'                                 °
  433.   '''When defining utility functions, there are several considerations t       °
  434.  o'''                                                                          °
  435.   '''keep in mind. A utility has EDGE EFFECTS, DEFAULTS, and ABORTS.'',r       °
  436.  '                                                                             °
  437.   '''By edge effects, I mean special cases that one does not normally'''       °
  438.   '''consider a solution to that particular utility. Let us assume that'       °
  439.  '' '''we desire to treat function MOVAVG as a utility. Then'',r'              °
  440.   'show ''1 MOVAVG ∞6'' ''6 MOVAVG ∞6'''                                       °
  441.   '''The first expression returns the original argument unchanged, while       °
  442.  ''' '''the second expression returns the average.''' 'do'                     °
  443.   '''The main difference between the second expression, and AVERAGE ∞6''       °
  444.  ' '''is in the rank of the result'',r'                                        °
  445.   'show ''DISPLAY (+/∞6)÷6'' ''DISPLAY 6 MOVAVG ∞6'''                          °
  446.   '''These examples are not normally thought of as ''''moving averages''       °
  447.  '', but''' '''are acceptable.''' 'do'                                         °
  448.   '''Consider now the following expressions'',r'                               °
  449.   'show ''DISPLAY 7 MOVAVG ∞6'' ''²2 MOVAVG ∞6'''                              °
  450.   '''While one could argue that these two examples are valid, it is not'       °
  451.  '' '''likely one would consider the results as reasonable. Function'''        °
  452.   '''MOVAVG, therefore, has some EDGE EFECTS that one might want to remo       °
  453.  ve.''' 'do'                                                                   °
  454.   '''Another problem is one of DEFAULTS. Suppose for the sake of argumen       °
  455.  t'''                                                                          °
  456.   '''that whenever we talk about moving averages, we normally assume tha       °
  457.  t'''                                                                          °
  458.   '''we are dealing with a specific value of the left argument, say 2.''       °
  459.  '                                                                             °
  460.   '''Then it might be reasonable to request the omission of the left'''        °
  461.   '''argument to signify a default value of 2. In other words whenever''       °
  462.  ' '''one enters'',r,r,''       MOVAVG ∞6'',r'                                 °
  463.   '''the result should be'',r,r,''1.5 2.5 3.5 4.5 5.5''' 'do'                  °
  464.   '''Finally, there may be expressions that cause the execution to be'''       °
  465.   '''interrupted with an error message'',r'                                    °
  466.   'show ''8 MOVAVG ∞6'' ''1.5 MOVAVG ∞6'''                                     °
  467.   '''My personal approach in this situation is to avoid, or postpone'''        °
  468.   '''the interrupt. For example, instead of the error message, the'''          °
  469.   '''function prints on the screen a character string, such as'',r'            °
  470.   '''       You have entered an illegal argument in function MOVAVG'',r'       °
  471.   '''and then returns a value that is clearly invalid (e.g. ∞0).'''            °
  472.   '''Any subseqent handling of the result can be identified by the calli       °
  473.  ng'''                                                                         °
  474.   '''function, and remedial action be taken without an interrupt.'''           °
  475.   'do'                                                                         °
  476.   '''This is the end of my EDITORIAL COMMENT. I am well aware that this        °
  477.  is'''                                                                         °
  478.   '''NOT the approach taken by other APL users. Some feel strongly that'       °
  479.  ''                                                                            °
  480.   '''any improper invocation of a user defined function should cause an'       °
  481.  ''                                                                            °
  482.   '''interrupt and display an error message. Ultimately the decision on'       °
  483.  ''                                                                            °
  484.   '''how to deal with such edge effects, defaults and aborts will depend       °
  485.   on''' '''who gets to use the final product.''' 'do'                          °
  486.   '''We will now see how these objectives can be achieved by refining th       °
  487. Xe''' '''function definition of MOVAVG.''' 'endd'                              °
  488. *(1997 8 30 11 51 48 660)                                                      °
  489.  FTEACH6 ÉFX 'TEACH6;T;FN;MOVAVG' 'Σ Modifying a defined function'             °
  490.   'exit ''TEACH6'''                                                            °
  491.   'FN╜''U╜L MOVAVG DATA'' ''Σ Moving average of length L on DATA'''            °
  492.   'FN╜FN,''U╜L+/DATA'' ''U╜U÷L''' 'T╜ÉFX FN'                                   °
  493.   '''Once a function has been defined, it is possible to re-enter the'''       °
  494.   '''function definition mode with ╖MOVAVG[É]. This will produce a listi       °
  495.  ng'''                                                                         °
  496.   '''of the function, followed by a prompt to enter a new line'',r'            °
  497.   'showfn ''MOVAVG''' '''[4]''' 'do'                                           °
  498.   '''At this point additional lines can be added to the function. Since'       °
  499.  ''                                                                            °
  500.   '''we do not wish to add new lines following line [3], we can insert''       °
  501.  ' '''lines by entering fractional line numbers'',r' '''[4] [1.1]'''           °
  502.   '''[1.1] ╕(Lε∞²1╞µDATA)/L0'''                                                °
  503.   '''[1.2] ''''You have entered an illegal argument in function MOVAVG''       °
  504.  '' ╪ U╜∞0 ╪ ╕0''' '''[1.3][2]''' '''[2] L0:U╜L+/DATA╖'',r'                    °
  505.   'T╜''''''You have entered an illegal argument in function MOVAVG'''' ╪       °
  506.   U╜∞0 ╪ ╕0''' 'FN╜(2╞FN),Γ''╕(Lε∞²1╞µDATA)/L0'''                              °
  507.   'FN╜FN,T ''L0:U╜L+/DATA'' ''U╜U÷L''' 'T╜ÉEX ''MOVAVG''' 'T╜ÉFX FN'           °
  508.   '''We can now look at the modified function with ╖RUNAVG[É]╖'''              °
  509.   '1 showfn ''MOVAVG'''                                                        °
  510.   'r,''The expression "╖MOVAVG[É]╖", beginning and ending with ╖ has the       °
  511.  '''                                                                           °
  512.   '''effect of entering the function definition mode, listing the functi       °
  513.  on,'''                                                                        °
  514.   '''and exiting the function definition mode. It is more useful than th       °
  515.  e'''                                                                          °
  516.   '''simpler ÉCR ''''MOVAVG'''' System Function in that it also provides       °
  517.   the''' '''line numbering.''' 'do'                                            °
  518.   '''The modified function MOVAVG now filters out invalid left arguments       °
  519.  '',r' 'show ''²2 MOVAVG ∞6'' ''1.5 MOVAVG ∞6'''                               °
  520.   '''The next step is to allow MOVAVG to also support a monadic form.'''       °
  521.   '''Whenever a function has that property, it is called AMBIVALENT.'''        °
  522.   '''On a defined function, this can be done by testing whether the'''         °
  523.   '''left argument is defined on entry to the function. This is done'''        °
  524.   '''with the ÉNC System Function''' 'showfn ''MOVAVG''' '''[8]''' 'do'        °
  525.   '''We will insert a test to that effect'',r' '''[8] [1.1]'''                 °
  526.   '''[1.1] ╕(2=ÉNC ''''L'''')/L1 ╪ L╜2'''                                      °
  527.   '''[2] L1:╕(Lε∞²1╞µDATA)/L0╖'''                                              °
  528.   'T╜(2╞FN),''╕(2=ÉNC ''''L'''')/L1 ╪ L╜2'' ''L1:╕(Lε∞²1╞µDATA)/L0'''          °
  529.   'FN╜T,3╟FN' 'T╜ÉEX ''MOVAVG''' 'T╜ÉFX FN' 'do'                               °
  530.   '''Let us look at this new version. Note the usage of the diamond to d       °
  531.  eal''' '''with the missing left argument'',r' '1 showfn ''MOVAVG'''           °
  532.   'r,''Applying MOVAVG monadically, we get a default 2 for left argument       °
  533. X'',r' 'show ''2 MOVAVG ∞6'' ''MOVAVG ∞6''' 'endd'                             °
  534. *(1997 6 17 13 4 55 448)                                                       °
  535.  FTEACH7 ÉFX 'TEACH7' 'Σ The session Manager editor' 'exit ''TEACH7'''         °
  536.   '''     THE SESSION MANAGER EDITOR'',r'                                      °
  537.   '''Historically, the DEL (╖) editor is the oldest method of defining''       °
  538.  ' '''APL functions. The Character Representation (ÉCR) was an early'''        °
  539.   '''improvement that made it possible to store user defined functions''       °
  540.  '                                                                             °
  541.   '''as character arrays in object libraries (See later tutorials dealin       °
  542.  g''' '''with Auxilliary Processors 210 and 211.)'',r'                         °
  543.   '''The most recent development is an editor that is based on OS2. It i       °
  544.  s'''                                                                          °
  545.   '''called the Session Manager Editor. To invoke it, you simply press o       °
  546.  n'''                                                                          °
  547.   '''the EDIT pull down menu, and select "Open Object". A window will ap       °
  548.  pear'''                                                                       °
  549.   '''that is set to a user defined function default. It sets the prompt'       °
  550.  ''                                                                            °
  551.   '''in a box where you are expected to enter the name of the function.'       °
  552.  ''                                                                            °
  553.   '''However, if the function is already defined, you can save yourself        °
  554.  the'''                                                                        °
  555.   '''effort of typing it by scrolling down on the "Objects" box and clic       °
  556.  king'''                                                                       °
  557.   '''on the desired function name (That name will appear on the top box)       °
  558.  '',r'                                                                         °
  559.   '''At that point you can either double click on the name, or press the       °
  560.   "OK"'''                                                                      °
  561.   '''button. The session manager editor allows you to insert new lines,        °
  562.  to'''                                                                         °
  563.   '''change existing lines and to move text (to and from the current'''        °
  564.   '''document or any other document accessible from OS2.)''' 'do'              °
  565.   '''Note also the three buttons under the label "Type of Object". You c       °
  566.  an'''                                                                         °
  567.   '''switch between a character array or a function. (We will defer the'       °
  568.  ''                                                                            °
  569.   '''"Operator" option until such time this feature is discussed in a la       °
  570.  ter'''                                                                        °
  571.   '''tutorial session.) There is only one constraint. If a function has        °
  572.  been'''                                                                       °
  573.   '''defined, a character array cannot have that same name. Note also th       °
  574.  at'''                                                                         °
  575.   '''the Session Manager Editor can be used to create non-APL documents        °
  576.  by'''                                                                         °
  577.   '''taking advantage of the "COPY" and "PASTE" options in the pull down       °
  578. X''' '''editor.''' 'endd'                                                      °
  579. *(1997 9 7 12 55 44 576)                                                       °
  580.  FTEACH8 ÉFX 'TEACH8;AVERAGE;T' 'Σ Headers' 'exit ''TEACH7'''                  °
  581.   '''We have seen examples of monadic and dyadic defined functions.'''         °
  582.   '''We have also seen how a dyadic function can be made to behave as'''       °
  583.   '''if it were monadic by making it ambivalent. Since the nature of'''        °
  584.   '''arguments to a defined function is specified in the header, it is''       °
  585.  ' '''only natural to ask what other headers are possible.''' 'do'             °
  586.   '''There are two other variations to headers: First, headers need not'       °
  587.  ''                                                                            °
  588.   '''have any arguments. Function without arguments are called NILADIC.'       °
  589.  ',r'                                                                          °
  590.   '''Niladic functions are typically used to provide explanations, or'''       °
  591.   '''as main entries to applications. We will give specific examples in'       °
  592.  '' '''future lessons of niladic functions.''' 'do'                            °
  593.   '''The other feature of headers deals with results. So far, our sample       °
  594.  '''                                                                           °
  595.   '''functions had a header of the form U╜... . Whenever a header contai       °
  596.  ns'''                                                                         °
  597.   '''such an assignment arrow, it is generally expected that the named''       °
  598.  ' '''variable be generated in the body of the function before exit.'''        °
  599.   '''Failure to do so will cause an error condition whenever an attempt'       °
  600.  ''                                                                            °
  601.   '''is made to assign the nonexistent result. (Some APL2 users will'''        °
  602.   '''intentionally leave the result undefined to force an error whenever       °
  603.  ''' '''the function is used improperly.)'',r'                                 °
  604.   'T╜ÉFX ''U╜AVERAGE R'' ''Σ Average of last dimension'' ''U╜+/R'' ''U╜Y       °
  605.  ÷0¥µR''' '''     ATTRIBUTES'',r'                                              °
  606.   '''There is a System function that provides information about the'''         °
  607.   '''ATTRIBUTES of a user defined function. ÉAT is dyadic. The left'''         °
  608.   '''argument specifies the type of information to be retrieved. The'''        °
  609.   '''right argument is the name (or array of names) to be searched.'''         °
  610.   '''The attributes that can be obtained fall into 4 categories:'',r'          °
  611.   ''' 1 Valences (the nature of the headers of the function)'''                °
  612.   ''' 2 Time when the user defined function was last fixed'''                  °
  613.   ''' 3 Whether the function is locked, displayable ....'''                    °
  614.   ''' 4 Size of the character object (used with shared variables)'',r'         °
  615.   '''Earlier we have defined function AVERAGE (it is re-defined here).''       °
  616.  ,r' 'show ''ÉCR '''' AVERAGE '''''''                                          °
  617.   '''Let us look at its attributes:'',r'                                       °
  618.   'show ''1 ÉAT ''''AVERAGE'''''''                                             °
  619.   '''The vector 1 1 0 means the function produces a result (1), is monad       °
  620.  ic''' '''(1) and is a function (0)'',r'                                       °
  621.   'show ''2 ÉAT ''''AVERAGE'''''''                                             °
  622.   '''The result gives the date and time when this function was fixed.'',       °
  623.  r' 'show ''3 ÉAT ''''AVERAGE'''''''                                           °
  624.   '''the vector indicates that this function is displayable, suspendable       °
  625.  ,'''                                                                          °
  626.   '''interruptible, and displays non-resource errors. (These subjects wi       °
  627.  ll'''                                                                         °
  628.   '''not be extensively discussed in these introductory tutorials .)'',r       °
  629.  ' 'show ''4 ÉAT ''''AVERAGE'''''''                                            °
  630.   '''The resulting vector indicates the function is a user defined funct       °
  631. Xion''' 'endd'                                                                 °
  632. *(1997 6 20 12 52 7 456)                                                       °
  633.  FTEACH9 ÉFX 'TEACH9;FN;TEST;TESTA;TESTB;TESTC;U;LEFT;RIGHT;T;F;G;H;I'         °
  634.   'Σ Local variables' 'exit ''TEACH8'''                                        °
  635.   '''LOCALIZATION OF VARIABLES'',r'                                            °
  636.   '''In the header for function RUNAVG, there are four labels'',r'             °
  637.   '''       U L RUNAVG and DATA'',r'                                           °
  638.   '''The labels U L and DATA are reserved names of variables. All header       °
  639.  '''                                                                           °
  640.   '''labels in a defined function have a SCOPE and a STATUS. The scope c       °
  641.  an be'''                                                                      °
  642.   '''LOCAL or GLOBAL, and the status can be DEFINED or UNDEFINED. Let us       °
  643.  ''' '''now study these concepts.''' 'do'                                      °
  644.   '''Let us investigate a defined function we shall call TESTA'',r'            °
  645.   'show ''FN╜''''LEFT TESTA RIGHT'''' ''''DISPLAY LEFT RIGHT G'''' ''''G       °
  646.  ╜''''''''global''''''''''''''' 'show ''ÉFX FN'''                              °
  647.   '''Let us first assign values to LEFT RIGHT and G'',r'                       °
  648.   'show ''(LEFT RIGHT G)╜''''SIX'''' ''''SEVEN'''' ''''GLOBAL'''''' ''DI       °
  649.  SPLAY LEFT RIGHT G''' '1 showfn ''TESTA''' ''''''                             °
  650.   'show ''6 TESTA 7'' ''DISPLAY LEFT RIGHT G'''                                °
  651.   '''Within function TESTA the "value" of LEFT and RIGHT is 6 and 7 resp       °
  652.  ectively.'''                                                                  °
  653.   '''The "value" of G remains ''''GLOBAL'''' because G is not listed in        °
  654.  the header'''                                                                 °
  655.   '''of TESTA. On exit from function TESTA the value of variables LEFT a       °
  656.  nd'''                                                                         °
  657.   '''RIGHT reverts to their global setting. By re-defining G within func       °
  658.  tion''' '''TESTA, we changed its content in the global variable G.'''         °
  659.   'do'                                                                         °
  660.   '''Any variables not specified in the header of a function are called        °
  661.  global.'''                                                                    °
  662.   '''Any global variable having the same name as a variable in a header        °
  663.  of a''' '''function cannot be reached from within that function.'',r'         °
  664.   '''Suppose now, we wish to define an additional label in a function, s       °
  665.  o that'''                                                                     °
  666.   '''it too is local. APL2 supports such a localization in the header'',       °
  667.  r'                                                                            °
  668.   'FN╜''TESTB;F G H I'' ''I╜π,■''''FGHI'''''' ''DISPLAY I'' ''ÉNC I'' ''       °
  669.  H╜4''' 'show ''πFN'''                                                         °
  670.   '''We have defined a NILADIC function named TESTB. The header includes       °
  671.   a'''                                                                         °
  672.   '''semicolon followed by a list of labels (F G H and I). These now def       °
  673.  ine''' '''APL2 objects that are strictly local to TESTB.'''                   °
  674.   '''(Note that ÉNC accepts rank 2 character arrays of labels)'',r'            °
  675.   'do' 'FN╜FN,''G╜''''U╜F H'''' ''''U╜∞H'''''' ''ÉFX G'' ''F H'''              °
  676.   'show ''πFN'''                                                               °
  677.   '''We have added three more lines to function TESTB. Variable G become       °
  678.  s a'''                                                                        °
  679.   '''nested vector defining F as a monadic function with argument H. Nex       °
  680.  t,'''                                                                         °
  681.   '''G is fixed defining function F. F becomes a function local to TESTB       °
  682.  .''' '''The final line of function TESTB evaluates function F.'',r'           °
  683.   'show ''ÉFX FN''' '1 showfn ''TESTB'''                                       °
  684.   '''Before we proceed with the evaluation of TESTB, you want to study''       °
  685.  ' '''carefully the listing of function TESTB.''' 'do'                         °
  686.   '''Before we evaluate TESTB, we will also define GLOBAL variables F, G       °
  687.  ,''' '''H and I'',r'                                                          °
  688.   'show ''(F G H I)╜''''ONE'''' ''''TWO'''' ''''THREE'''' ''''FOUR''''''       °
  689.   ''DISPLAY F G H I''' 'show ''TESTB'' ''πF G H I'''                           °
  690.   '''In summary, the header can be used to define not only the nature of       °
  691.  '''                                                                           °
  692.   '''the function (monadic, dyadic, niladic and with or without result),       °
  693.  '''                                                                           °
  694.   '''but also any labels that are to be stricly local to that function.'       °
  695.  ''                                                                            °
  696.   '''These localized object names can be used to define any valid APL2''       °
  697.  ' '''objects.''' 'do'                                                         °
  698.   '''Before we leave the subject of headers, there is one other aspect''       °
  699.  '                                                                             °
  700.   '''of its use. When the APL2 interpreter reads the header in order to'       °
  701.  ''                                                                            °
  702.   '''allocate and match arguments to names, it does so from left to righ       °
  703.  t'',r'                                                                        °
  704.   'show ''ÉFX ''''U╜U TESTC U'''' ''''U'''' ''''U╜0'''''' ''5 TESTC ∞4''       °
  705.  '                                                                             °
  706.   '''In the header, the label repeats 3 times. As the header is evaluate       °
  707.  d,'''                                                                         °
  708.   '''the leftmost argument, the asigned variable is left undefined. Then       °
  709.  '''                                                                           °
  710.   '''the left argument of TESTC, if present is assigned to U. Finally th       °
  711.  e'''                                                                          °
  712.   '''right argument of TEST is assigned to U. Knowing this sequence allo       °
  713.  ws''' '''definition of default results in some applications'',r'              °
  714.   'show ''ÉFX ''''R╜L TEST R'''' ''''╕(0=ÉNC ''''''''L'''''''')/0'''' ''       °
  715.  ''R╜R+L''''''' 'show ''ÉCR ''''TEST'''''' ''3 TEST 4'' ''TEST 5'''            °
  716. X 'endd'                                                                       °
  717. *(1997 7 13 12 28 49 504)                                                      °
  718.  Faddquote ÉFX 'u╜addquote w'                                                  °
  719.   'Σ Put quotes around a string, and double existing quotes'                   °
  720. X 'u╜ÉAV[40],((1+w=ÉAV[40])/w),ÉAV[40]'                                        °
  721. *(1997 7 24 13 20 38 476)                                                      °
  722.  Fav ÉFX 'av;A;N;I;ÉIO' 'Σ Display characters in ÉAV' 'ÉIO╜0'                  °
  723.   'A╜22 78µ'' ''' 'N╜3 0«φ12 22µ1+∞356' 'A[;,(6⌡∞12)°.+2 3 4]╜N'               °
  724.   'A[;6+6⌡∞12]╜φ12 22µÉAV' 'ΣA[8 10 13;6]╜'' ''' 'A[13;6]╜'' '''               °
  725. X 'A[14+∞8;68 69 70 72]╜'' ''' 'A'                                             °
  726. *(1991 11 11 8 25 13 316)                                                      °
  727.  Fdate ÉFX 'u╜date w' 'Σ Format date and time of day' 'u╜«6╞w'                 °
  728. X 'u╜('' ''⌠u)Γu' 'u╜εu,■''-- .. '''                                           °
  729. XCdig 1 10 1234567890                                                          °
  730. *(1997 9 9 13 0 45 372)                                                        °
  731.  Fdisclaimer ÉFX 'disclaimer' 'Σ Copyright statement'                          °
  732.   '(10µ'' ''),''Copyright, Z. V. Jizba, 1995,1996,1997'',r'                    °
  733.   ''' This and subsequent workspaces labelled TEACHxx are made available       °
  734.  '''                                                                           °
  735.   '''at no cost to anyone who desires to learn how to use effectively'''       °
  736.   '''the IBM/OS2 version of APL2.'',r'                                         °
  737.   '''This software is provided "AS IS" with no WARRANTY of any kind, eit       °
  738.  her'''                                                                        °
  739.   '''express or implied. Any risk in its use resides with you, the user        °
  740.  of''' '''these tutorials.'',r' '''     ACKNOWLEDGEMENTS'',r'                  °
  741.   ''' In writing these tutorials, I am greatly indebted to Roy Sykes, wh       °
  742.  ose'''                                                                        °
  743.   '''excellent lectures increased my understanding of the language.'''         °
  744.   '''Discussions with the late Harry Bertucelli clarified a number of'''       °
  745.   '''concepts and caused me to change some improper terminology that was       °
  746.  '''                                                                           °
  747.   '''used in previous versions of these tutorials. Mr. Benjamin Archer''       °
  748.  '                                                                             °
  749.   '''kindly checked out a nearly final version, bringing to my attention       °
  750.  ''' '''some ommisions, misspellings, and invalid terminology.'',r'            °
  751. X '''(PRESS ENTER to continue)'''                                              °
  752. *(1997 7 13 12 28 50 508)                                                      °
  753.  Fdo ÉFX 'do;T;E' 'Σ Expression driver' 'E╜''''' 'æ╜''      ''' 'T╜æ'          °
  754.   '╕(^/'' ''=T)/0'                                                             °
  755.   '╕(('':''εT)doif ''B╜evaldd (+/^\'''' ''''=T)╟T'')/2'                        °
  756.   '''E╜ÉEM'' ÉEA T' '╕(0=µ,E)/2'                                               °
  757.   '''This is not a valid APL2 expression''' 'æ╜''*''' '╕(''?''⌠╞1╟æ)/2'        °
  758. X 'E' '╕2'                                                                     °
  759. *(1997 7 13 12 28 50 508)                                                      °
  760.  Fdoif ÉFX 'U╢╜V╢ doif W╢;t╢' 'Σ Rule' '╕(^/~U╢╜V╢)/0'                         °
  761. X '''U╢╜V╢ doif■ W╢'' ÉEA ''»V╢/W╢'''                                          °
  762. *(1997 9 9 12 50 14 444)                                                       °
  763.  Fendd ÉFX 'endd' 'Σ end of special feature' '20µ''²'' ╪ ╕(4<µÉLC)/0'          °
  764. X 'do'                                                                         °
  765. *(1997 8 16 12 11 1 260)                                                       °
  766.  Ferase ÉFX 'erase;t;EXIT;GO;HELP;BIND;DISPLAY;REVIEW;RULE;DISCLAIMER'         °
  767.   'Σ Erase all global functions and variables' 't╜ÉNL 3'                       °
  768.   't╜(~t^.εlc,'' '')≡t' 't╜ÉEX(~t[;∞5]^.=''TEACH'')≡t' 't╜ÉNL 2'               °
  769. X 't╜ÉEX(~t^.εlc,'' '')≡t'                                                     °
  770. *(1997 7 27 13 47 41 608)                                                      °
  771.  Fevaldd ÉFX 'u╜evaldd w;c;n' 'Σ Evaluate direct definition' 'u╜0'             °
  772.   'n╜(w∞''Σ'')-1' 'c╜(((n╞w)⌠'':'')Γn╞w),Γ''ΣDD '',(n+1)╟w'                    °
  773.   '╕((1 label╞c)doif ''''''Invalid label'''''')/0'                             °
  774.   '╕((2=µc)doif ''u╜showdd 1╙c'')/0'                                           °
  775.   '╕((3=ÉNC╞c)doif ''u╜⌡µÉ╜(╞c),'''' is already defined.'''''')/0'             °
  776.   '╕((3=µc)doif ''u╜simdd c'')/0' 'c╜(Γ''α∙ aw'')replace■c'                    °
  777.   'u╜ε''u╜'',((''a''εεc[2 3 4])/''a ''),(╞c),'' w;t;b'''                       °
  778.   'u╜u(5πc)(''b╜(t╜'',(3πc),'')/'',addquote ''u╜'',4πc)'                       °
  779. X 'u╜u,''╕(t doif b)/0''(''u╜'',2πc)' 'u╜╧ÉFX u'                               °
  780. *(1997 7 25 13 27 52 564)                                                      °
  781.  Fexit ÉFX 'V exit W;T' 'Σ Exit if too many suspended functions'               °
  782.   '╕(0⌠ÉNC ''V'')/L0 ╪ V╜10' 'L0:╕(V>µÉLC)/0'                                  °
  783.   '''There are too many suspended functions''' '''Please enter '',W'           °
  784. X '╕'                                                                          °
  785. *(1997 7 26 12 33 39 536)                                                      °
  786.  Fget ÉFX 'U╜V get W;t;T;ÉPR' 'Σ Prompt for response from keyboard'            °
  787.   'ÉPR╜T╜ÉAV[ÉIO+255] ╪ ╕(0⌠ÉNC ''V'')/L0 ╪ V╜1' 'L0:V╜V╧1' 'æ╜W ╪ t╜æ'        °
  788.   'U╜(+/^\t=T)╟t' '╕(''╕''⌠╞U)/L1 ╪ ╕' 'L1:╕V/0' 't╜(U⌠'' '')ΓU'               °
  789. X 'U╜(µt),(ΓU),t'                                                              °
  790. *(1997 7 28 13 33 8 424)                                                       °
  791.  Fglobals ÉFX 'globals' 'Σ Initialize useful global variables'                 °
  792.   'uc╜''ABCDEFGHIJKLMNOPQRSTUVWXYZ'''                                          °
  793.   'lc╜''abcdefghijklmnopqrstuvwxyz''' 'dig╜''1234567890'''                     °
  794. X 'r╜ÉAV[13+ÉIO]' 'q╜'''''''''                                                 °
  795. *(1997 7 3 12 47 6 368)                                                        °
  796.  Finitialize ÉFX 'initialize;T' 'Σ Initialize workspace'                       °
  797.   '''AT ALL TIMES, TO CONTINUE, PRESS RETURN!'',r'                             °
  798.   '''To see disclaimers enter:'',r,''    disclaimer''' 'do' 'erase'            °
  799.   'globals'                                                                    °
  800.   '''Make sure the CAP LOCK light on your keyboard (upper right) is ON!'       °
  801. X'' 'endd'                                                                     °
  802. *(1997 7 27 13 14 33 444)                                                      °
  803.  Flabel ÉFX 'u╜v label w'                                                      °
  804.   'Σ Return 1 if label w does not begin with a cap'                            °
  805.   '╕(0⌠ÉNC ''v'')/L0 ╪ v╜0' 'L0:v╜v╧1 ╪ w╜εw ╪ ╕v/L1 ╪ ╕(u╜0⌠ÉNC w)/0'         °
  806. X 'L1:╕(u╜~^/wεlc,uc,dig)/0' 'u╜w[1]εlc,dig'                                   °
  807. XClc 1 26 abcdefghijklmnopqrstuvwxyz                                           °
  808. *(1997 7 13 12 28 55 528)                                                      °
  809.  Fnon ÉFX 'non;T;RC;ET;R' 'Σ Ignore keyboard entry' 'æ╜''      ''' 'T╜æ'       °
  810.   '╕(0=µ(T⌠'' '')/T)/0' '(RC ET R)╜ÉEC T' '╕(0=RC)/2'                          °
  811. X '╕((1=RC)doif ''R'')/2' '╕2'                                                 °
  812. *(1997 7 13 12 28 55 528)                                                      °
  813.  Fnotb ÉFX 'u╜notb w' 'Σ Remove trailing blanks'                               °
  814.   '╕((1<╧w)doif ''u╜notb■ w'')/0' '╕((1<µµw)doif ''u╜πnotb Γ[2]w'')/0'         °
  815. X 'u╜(1-(,'' ''⌠Φw)∞1)╟w'                                                      °
  816. *(1997 7 27 12 55 6 496)                                                       °
  817.  Fproblems ÉFX 'problems' 'Σ Problems'                                         °
  818.   '''That is all for this lesson. Remember, if you want to practice,'''        °
  819.   '''and plan to use direct definitions, be sure to first enter GO.'''         °
  820.   '''Direct definitions will then be accepted. To exit GO, enter EXIT.''       °
  821.  ,r'                                                                           °
  822.   '''To erase a previously defined DIRECT DEFINITION FUNCTION, enter'',r       °
  823.  ' '''       )ERASE functionname'',r'                                          °
  824. X '''WARNING! do not use )ERASE on other labels.'',r'                          °
  825. XCq 0 '                                                                        °
  826. XCr 0                                                                         °
  827. *(1997 7 13 12 28 56 532)                                                      °
  828.  Freplace ÉFX 'u╜v replace u;i;r;s' 'Σ Replace elements in v in u'             °
  829.   'i╜Γ∞µu' 's╜2πv╜(v⌠'' '')Γv' 'i╜⌡r╜i⌡■Γ[1]u°.=╞v'                            °
  830. X 'u[(εi)/εr]╜s[(εi)/εi⌡■∞µs]'                                                 °
  831. *(1997 7 13 12 28 56 532)                                                      °
  832.  Fround ÉFX 'U╜V round W' 'Σ Half adjust to V th decimal'                      °
  833. X 'U╜(╛0.5+W⌡10*V)÷10*V'                                                       °
  834. *(1997 7 13 12 28 57 536)                                                      °
  835.  Fshow ÉFX '╢V show ╢W;╢T;╢B' 'Σ Display and execute ╢W'                       °
  836.   '╢T╜(0=ÉNC ''╢V'')doif ''╢V╜0'''                                             °
  837.   '╕((0=╧╢W)doif ''show ╢W,'''' '''''')/0'                                     °
  838.   '╕((1<╧╢W)doif ''╢V show ■╢W'')/0' '''      '',╢W'                           °
  839. X '╕((╢V^'':''ε╢W)doif ''╢T╜evaldd ╢W'')/L0' '''ÉEM'' ÉEA ╢W' 'L0:do'          °
  840. *(1997 7 13 12 28 57 536)                                                      °
  841.  Fshowdd ÉFX 'u╜showdd w;a;b;c;r'                                              °
  842.   'Σ Display a direct definition function'                                     °
  843.   '╕((1=╧w)doif ''u╜showdd Γw'')/u╜0'                                          °
  844.   '╕((3⌠ÉNC╞w)doif ''(ε╞w),'''' is not a function'''''')/0'                    °
  845.   'c╜Γ[2]ÉCR╞w' 'c╜notb(2╞c),(Γ''aw α∙'')replace■2╟c'                          °
  846.   '╕((~''ΣDD''╧3╞2πc)doif ''''''Not a direct definition function'''''')/       °
  847.  0' 'u╜1' 'b╜('' ''⌠╞c)Γ╞c' 'a╜''      ''' 'r╜2╟3πc'                           °
  848.   '╕((3=µc)doif ''a,(╞w),'''':'''',r,(3<µ2πc)/''''   Σ'''',3╟2πc'')/0'         °
  849.   'a╜a,(╞w),'':'',(2╟5πc),'':''' 'b╜(+\r=''('')-+\r='')''' 'b╜b∞0'             °
  850. X 'a╜a,(²3╟(b-1)╞3╟r),'':'',2╟»(b+2)╟r' 'a,(3<µ2πc)/''  Σ'',3╟2πc'             °
  851. *(1997 7 13 12 28 57 536)                                                      °
  852.  Fshowfn ÉFX 'U╜V showfn W;F;N;T;ÉIO' 'Σ Simulate ╖W[É]'                       °
  853.   'T╜(0=ÉNC ''V'')doif ''V╜0''' 'ÉIO╜0'                                        °
  854.   'U╜r,''      '',''╖'',W,''[É]'',(╞V)╞''╖''' 'N╜1╞µF╜ÉCR W' 'N╜«∞N'           °
  855.   'N╜(N⌠'' '')ΓN' 'F╜(π''['',■N,■Γ''] ''),F'                                   °
  856.   'T╜(1<µ,V)doif ''F╜F[1╟V;]'' ''U╜''''''''''' 'U╜²1╟U,r,,F,r'                 °
  857. X 'U╜((-+/^\'' ''=ΦU)╟U),('' ╖'')[╞V],r'                                       °
  858. *(1997 7 13 12 28 58 540)                                                      °
  859.  Fsimdd ÉFX 'u╜simdd w;e' 'Σ Direct definition mode' 'u╜0'                     °
  860.   '╕((0⌠ÉNC╞w)doif ''''''Already defined'''''')/0' 'e╜''α''ε2πw'               °
  861.   'w[2]╜Γ''u╜'',''α∙ aw'' replace 2πw' 'w╜w[1 3 2]'                            °
  862. X 'w[1]╜Γε''u╜'',(e/''a ''),w[1],'' w''' 'u╜╧ÉFX w'                            °
  863. *(1992 6 3 9 59 17 424)                                                        °
  864.  Ftab ÉFX 'U╜V tab W;T;A;B;C;D;E;F;G;M;ÉPW' 'Σ Tabulate list W'                °
  865.   'T╜(0=ÉNC ''V'')doif ''V╜0''' 'M╜''Invalid data for tabulation'''            °
  866.   'V╜4╞V' 'ÉPW╜130╛30⌐G╜V[2]+79⌡V[2]=0'                                        °
  867.   'L1:╕((1<╧W)doif ''''''W╜∞0'''' ÉEA ''''W╜πW'''''')/L1'                      °
  868.   '╕(((0=µεW)δ2<µµW)doif ''U╜(~V╧4╞0)/M'')/0'                                  °
  869.   'T╜(1≥µµU╜«W)doif ''U╜πW╜(U⌠'''' '''')ΓU'''                                  °
  870.   'T╜(0<V[1])doif ''U╜(«(Φ1,╞µW)µ(V[3]µ'''' ''''),∞(╞µW)-V[3]),'''' ''''       °
  871.  ,U''' '╕(G<30)/0' 'T╜(F╜µεV[4])+C╜1╟B╜µA╜(V[3],0)╟U'                          °
  872.   'T╜⌐(1╞B)÷1⌐╛(ÉPW+F)÷T' 'U╜(E╜(V[3],C)╞U),[1](B╜T,1╟B)╞A'                    °
  873.   '''D╜εV[4]'' ÉEA ''D╜ÉAV[εV[4]+33⌡V[4]=0]''' 'L0:A╜(T,0)╟A'                  °
  874. X '╕(0=1╞µA)/0' 'U╜U,(((T+V[3]),µD)µD),E,[1]B╞A' '╕L0'                         °
  875. *(1997 7 13 12 28 59 544)                                                      °
  876.  Ftest ÉFX 'U╜V test W;P'                                                      °
  877.   'Σ Describe problem in W, (correct answer in V)' 'U╜2' 'L1:W'                °
  878.   'É╜''      ''' 'P╜æ' '''╕L0'' ÉEA ''P╜»P''' '╕(V╧P)/0'                       °
  879. X 'L0:╕(0=U╜U-1)/0' '''Incorrect. Try again''' '╕L1'                           °
  880. XCuc 1 26 ABCDEFGHIJKLMNOPQRSTUVWXYZ                                           °
  881. *(1996 4 6 16 5 22 276)                                                        °
  882.  Fvec ÉFX 'U╜vec' 'Σ Draw 2d vector in plane'                                  °
  883.   'U╜r,r,''2-        +'',r '':      °'',r,''1-    °'',r'                       °
  884. X 'U╜U,'' :  °'',r,''0|....|....|'',r,'' 0    1    2'',r'                      °
  885.