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

  1. XNÉIO 0 1                                                                      °
  2. XNÉCT 0 1E²13                                                                  °
  3. XCÉFC 1 6 .,*0_²                                                               °
  4. XNÉRL 0 1399399247                                                             °
  5. XCÉPR 1 1                                                                      °
  6. XCÉLX 1 5 TEACH                                                                °
  7. XNCOINS 1 5 0.01 0.05 0.1 0.25 0.5                                             °
  8. *(1997 8 4 14 0 49 368)                                                        °
  9.  FCOMPLEX ÉFX 'COMPLEX' 'Σ Explain complex numbers'                            °
  10.   '''The mysteries of square roots have been known to the ancient Greeks       °
  11.  .'''                                                                          °
  12.   '''It was Pythagoras or one of his colleagues who discovered that the        °
  13.  square'''                                                                     °
  14.   '''root of two could not be represented as the ratio of two integers.        °
  15.  It'''                                                                         °
  16.   '''took another 2000 years before mathematicians could tackle the prob       °
  17.  lem'''                                                                        °
  18.   '''of the square roots of negative numbers. In order to extend the num       °
  19.  ber'''                                                                        °
  20.   '''domain, mathematicians developed (invented?) the concept of complex       °
  21.  '''                                                                           °
  22.   '''numbers. These are number pairs, composed of a "real" number, and a       °
  23.  n'''                                                                          °
  24.   '''"imaginary" number. The "imaginary" component is a "real" number'''       °
  25.   '''multiplied by "i", the square root of ²1.'',r'                            °
  26.   '''In APL2, complex numbers are represented as a single "scalar". The'       °
  27.  ''                                                                            °
  28.   '''"real" and "imaginary" components are separated by the letter J.'''       °
  29.   '''For example, "i" is written in APL2 as 0J1.'',r'                          °
  30.   '''If you have no experience dealing with complex numbers, you may'''        °
  31.   '''ignore any and all references to them in these tutorials.'''              °
  32. X 'endd'                                                                       °
  33. *(1997 9 14 12 30 44 504)                                                      °
  34. XFDISCLAIMER ÉFX 'DISCLAIMER' 'Σ Copyright statement' 'disclaimer'             °
  35. *(1997 4 27 12 39 5 416)                                                       °
  36.  FDISPLAY ÉFX 'D╜S DISPLAY A;ÉIO;R;C;HL;HC;HT;HB;VL;VB;V;W;N;B'                °
  37.   'Σ DISPLAY A GENERAL ARRAY IN PICTORIAL FORM'                                °
  38.   'Σ  NORMAL CALL IS MONADIC.  DYADIC CALL USED ONLY IN'                       °
  39.   'Σ    RECURSION TO SPECIFY DISPLAY RANK, SHAPE, AND DEPTH.' 'ÉIO╜0'          °
  40.   '»(0=ÉNC ''S'')/''S╜µA''' 'R╜╞µ,S                   Σ PSEUDO RANK.'          °
  41.   'C╜''┐┌└┘''                 Σ UR, UL, LL, AND LR CORNERS.'                   °
  42.   'HL╜''─''                   Σ HORIZONTAL LINE.'                              °
  43.   'HC╜HL,''Θ╕'',HL,''~+ε''      Σ HORIZONTAL BORDERS.'                         °
  44.   'HT╜HC[(0<R)⌡1+0<╞²1╞,S]' 'ΣW╜,0╧■╞0µΓ(1⌐µA)╞A'                              °
  45.   'HB╜HC[3+3╛(''2⌡~A╧«A'' ÉEA ''1+╞ε0⌡(1⌐⌡/µA)╞,A'')+3⌡1<µµS]'                 °
  46.   'VL╜''│''                   Σ VERTICAL LINE.'                                °
  47.   'VB╜VL,''Φ╟''               Σ VERTICAL BORDER.'                              °
  48.   'V╜VB[(1<R)⌡1+0<²1╞²1╟,S]'                                                   °
  49.   '»(0εµA)/''A╜(1⌐µA)µΓ╞A''   Σ SHOW PROTOTYPE OF EMPTIES.'                    °
  50.   '╕(1<╧A)/GEN' '╕(2<µµA)/D3'                                                  °
  51.   'D╜«A                     Σ SIMPLE ARRAYS.' 'W╜1╞µD╜(²2╞1 1,µD)µD'           °
  52.   'N╜²1+1╟µD' '╕(0=µµA)/SS'                                                    °
  53.   'D╜(C[1],V,((W-1)µVL),C[2]),((HT,NµHL),[0]D,[0]HB,NµHL),C[0],(WµVL),C[       °
  54.  3]' '╕0' 'SS:HB╜((0 '' '')=╞0µΓA)/'' -'''                                     °
  55.   'D╜'' '',('' '',[0]D,[0]HB,Nµ'' ''),'' ''' '╕0'                              °
  56.   'GEN:D╜«DISPLAY■A          Σ ENCLOSED ...' 'N╜Dδ.⌠'' '''                     °
  57.   'D╜(Nδ~1ΦN)≡D' 'D╜(δ≡~''  ''╤D)/D' 'D╜((1,µS)µS)DISPLAY D'                   °
  58.   '╕(2≥µ,S)╟D3E,0' 'D3:D╜0 ²1╟0 1╟«ΓA         Σ MULT-DIMENSIONAL ...'          °
  59.   'W╜1╞µD' 'N╜²1+1╟µD'                                                         °
  60.   'D╜(C[1],V,((W-1)µVL),C[2]),((HT,NµHL),[0]D,[0]HB,NµHL),C[0],(WµVL),C[       °
  61.  3]' 'D3E:N╜²2+µ,S'                                                            °
  62. X 'V╜C[Nµ1],[0]VB[1+0<²2╟,S],[0](((²3+╞µD),N)µVL),[0]C[Nµ2]' 'D╜V,D'           °
  63. *(1997 4 27 12 39 5 416)                                                       °
  64.  FEXIT ÉFX 'EXIT' 'Σ Exit from function' '''To log off type:   )OFF'''         °
  65. X '╕'                                                                          °
  66. *(1996 4 6 15 59 48 592)                                                       °
  67.  FGO ÉFX 'GO;T;E;B' 'Σ Expression driver' 'L0:B╜E╜''''' 'æ╜''      '''         °
  68.   'T╜æ' '╕(^/'' ''=T)/L0' '╕((^/'')OFF ''=5╞6╟T)doif ''EXIT'')/0'              °
  69.   '╕(('':''εT)doif ''B╜evaldd (+/^\'''' ''''=T)╟T'')/L0'                       °
  70.   '''E╜ÉEM'' ÉEA T' '╕(0=µ,E)/L0' '╕B/L0'                                      °
  71.   '''This is not a valid APL2 expression''' 'æ╜''*'''                          °
  72. X '╕(''?''⌠╞1╟æ)/L0' 'E' '╕L0'                                                 °
  73. *(1997 8 12 12 20 18 348)                                                      °
  74.  FHELP ÉFX 'HELP;N;I;T' 'Σ Help to student' '''WHAT TO DO'',r'                 °
  75.   '''  ° To get out of the lesson''' '''       ENTER: EXIT'''                  °
  76.   '''  ° To log off APL2'''                                                    °
  77.   '''       FIRST, ENTER: EXIT  THEN ENTER: )OFF'''                            °
  78.   '''  ° To get help''' '''       ENTER: HELP'''                               °
  79.   '''  ° When you see the prompt on a blank line'''                            °
  80.   '''       ENTER AN APL2 EXPRESSION - OR JUST PRESS: ENTER''' 'do'            °
  81.   '''  ° If you get this line'',r'                                             °
  82.   '''This is not a valid APL2 expression'',r,''*'',r'                          °
  83.   '''        YOU CAN EITHER''' '''           A. PRESS: ENTER'''                °
  84.   '''           B. PRESS: ? and then ENTER to see what was incorrect'''        °
  85.   '''              in your expression causing that response'',r' 'do'          °
  86.   '''HINTS'',r'                                                                °
  87.   '''This lesson is made up of '',(«N╜8),'' components named TEACHx, whe       °
  88.  re''' '''the x stands for a digit:'',r' 'I╜0'                                 °
  89.   'L0:T╜''TEACH'',(«I╜I+1)' 'T,'' '',1╟notb(ÉCR T)[2;]' '╕(N>I)/L0'            °
  90.   'do'                                                                         °
  91.   '''You may review either of these components separately. To do that,''       °
  92.  '                                                                             °
  93.   '''first enter EXIT (and RETURN), then enter the name of the lesson.''       °
  94.  ' '''component (e.g. TEACH4).'',r'                                            °
  95.   '''To re-start the lesson, just enter TEACH'',r'                             °
  96.   '''When the screen fills up, it is a good idea to move the cursor to''       °
  97.  ' '''the start of a convenient paragraph, and press ENTER'',r'                °
  98.   '''You may also wish to press PAGE UP to review the prevous pages.'''        °
  99.   'do' '''RESOURCES'',r'                                                       °
  100.   '''You may also enter ONE of the following words at a time'',r'              °
  101.   '''RECIPROCAL Avoiding error message with ÷ when vector contains zeros       °
  102.  ''' '''KEYS       To display special symbols on the keyboard'''               °
  103.   '''RULES      Explain purpose in having RULES in this tutorial'''            °
  104. X '''RULE n     Additional comments on Rule n''' 'endd'                        °
  105. XNNUMBER 1 5 3 5 2 3 0                                                         °
  106. *(1997 9 15 12 35 48 544)                                                      °
  107.  FRECIPROCAL ÉFX 'RECIPROCAL;ZEROS'                                            °
  108.   'Σ Expression for reciprocal of vector with zeros'''                         °
  109.   '''Let us generate vector ZEROS:'',r'                                        °
  110.   'show ''ZEROS╜1 0 2 0 3 0 4 0 5'''                                           °
  111.   'show ''(1+99⌡ZEROS=0)÷ZEROS+0=ZEROS'''                                      °
  112.   '''Can you find a shorter expression?''' 'do'                                °
  113.   '''To get an expression such that ZEROS is the same as ÷÷ZEROS,'''           °
  114.   '''we merely change the constant:'',r'                                       °
  115.   'show ''ZEROS╜(1+²1⌡ZEROS=0)÷ZEROS+0=ZEROS'' ''ZEROS'''                      °
  116.   '''Replacing the 99 by ²1 any zero element again becomes zero. Now'''        °
  117.   '''applying the same expression to the new vector ZEROS,'',r'                °
  118.   'show ''(1+²1⌡ZEROS=0)÷ZEROS+0=ZEROS'''                                      °
  119. X '''we get the original vector ZEROS.''' 'endd'                               °
  120. *(1997 5 30 13 54 49 672)                                                      °
  121.  FRULE ÉFX 'V RULE W' 'Σ Display rules' '╕(2=ÉNC ''V'')/L0'                    °
  122. X '2 rule W ╪ ╕0' 'L0:V rule W'                                                °
  123. *(1997 8 5 12 40 15 388)                                                       °
  124.  FRULES ÉFX 'RULES' 'Σ Explain objective of rules'                             °
  125.   '''When studying a new subject, such as APL2, it may be convenient to'       °
  126.  ''                                                                            °
  127.   '''simplify a subject to enhance understanding. Rules are introduced''       °
  128.  '                                                                             °
  129.   '''here as a kind of scaffold to temporarily support the APL2 theory''       °
  130.  '                                                                             °
  131.   '''as it is explained in small increments. When the tutorials reach a'       °
  132.  ''                                                                            °
  133.   '''certain level of understanding of APL2, a more compact set of rules       °
  134.  '''                                                                           °
  135.   '''(called "bindings") will be discussed. At that point some of the'''       °
  136.   '''rules listed here initially can be discarded.'',r' 'do'                   °
  137.   '''Each time a rule is given, you may wish to enter the expression'',r       °
  138.  ' '''      RULE n         (where n is the rule number)'',r'                   °
  139.   '''When you do that, you will get additional comments on extensions to       °
  140.  '''                                                                           °
  141.   '''that rule in subsequent lessons. If you want to see BOTH the rule A       °
  142.  ND'''                                                                         °
  143.   '''any associated comments, enter a 1 in front of the word RULE. For''       °
  144.  ' '''example, to see rule #1 and comments, enter'',r,''  1 RULE 1'''          °
  145. X 'do' 'endd'                                                                  °
  146. *(1997 8 5 12 56 50 592)                                                       °
  147.  FTEACH ÉFX 'TEACH' 'Σ Start second lesson in APL2 by Zdenek V JIZBA'          °
  148.   'exit ''TEACH''' 'initialize' 'TEACH1 Σ Vector operations'                   °
  149.   'TEACH2 Σ The reduction operator' 'TEACH3 Σ Zeros and nulls'                 °
  150.   'TEACH4 Σ The DISPLAY function' 'TEACH5 Σ Error messages'                    °
  151.   'TEACH6 Σ Simple APL2 algorithm: average'                                    °
  152.   'TEACH7 Σ (Complex conjugate) Signum; avoiding divide by zero'               °
  153.   'TEACH8 Σ Roll and deal'                                                     °
  154.   '''That is all for this lesson. You can continue practicing what you''       °
  155.  '                                                                             °
  156.   '''have learned so far, but note that if you enter an invalid APL2'''        °
  157. X '''expression, you will see the APL2 Error Message.'''                       °
  158. *(1997 8 25 13 7 5 300)                                                        °
  159.  FTEACH1 ÉFX 'TEACH1' 'Σ Introduction to lesson 2' 'exit ''TEACH1'''           °
  160.   '''In the first lesson we introduced the concepts of SCALARS, VECTORS,       °
  161.  '''                                                                           °
  162.   '''and ARRAYS. We also discussed FUNCTIONS, VARIABLES and ASSIGNMENT.'       °
  163.  ''                                                                            °
  164.   '''We learned that functions can have only a right argument (MONADIC)'       °
  165.  '' '''or both a right and a left argument (DYADIC).'',r'                      °
  166.   '''We learned that APL2 supports only two data types: NUMBERS and'''         °
  167.   '''CHARACTERS. In this lesson we will expand on these concepts, and ad       °
  168.  d''' '''a few more.'',r' 'do'                                                 °
  169.   '''First, let us take another look at VECTORS. Consider the following'       °
  170.  '' '''specification (assignment) expression:'',r'                             °
  171.   'show ''COINS╜0.01 0.05 0.10 0.25 0.50'''                                    °
  172.   '''This vector represents the five most common denominator of US coins       °
  173.  :''' '''A penny, a nickel, a dime, a quarter, and a half dollar.'',r'         °
  174.   '''Next, we ask someone to tell us the amount of each kind of coin in'       °
  175.  ''                                                                            °
  176.   '''their posession. We will record this, and store it in variable NUMB       °
  177.  ER:'',r' 'show ''NUMBER╜3 5 2 3 0'''                                          °
  178.   '''The problem now is to calculate the total amount represented by thi       °
  179.  s'''                                                                          °
  180.   '''collection. We start by entering the following expression:'',r'           °
  181.   'show ''COINS⌡NUMBER'''                                                      °
  182.   '''Aha! We have uncovered two new aspects of APL2: First, the names of       °
  183.  '''                                                                           °
  184.   '''variables can be used as arguments in an expression. This should be       °
  185.  '''                                                                           °
  186.   '''no news if you have studied programming languages. However, one asp       °
  187.  ect'''                                                                        °
  188.   '''IS new, namely there was no requirement to write a code segment (su       °
  189.  ch'''                                                                         °
  190.   '''as a DO loop) to tell the computer how to evaluate that expression.       °
  191.  '''                                                                           °
  192.   '''So the first new aspect of APL2 is its ability to provide an answer       °
  193.  '''                                                                           °
  194.   '''without being told the procedure. This property of a computer langu       °
  195.  age'''                                                                        °
  196.   '''is called NON-PROCEDURAL. So at the level of primitive functions,''       °
  197.  ' '''APL2 is a NON-PROCEDURAL language.''' 'do'                               °
  198.   '''A second aspect of the expression COINS⌡NUMBER is the fact that'''        °
  199.   '''primitive functions can be applied to two vectors. As we will see''       °
  200.  '                                                                             °
  201.   '''there are strict, but reasonable limitations on this. Both COINS'''       °
  202.   '''and NUMBER are vectors of length 5. APL2 cannot handle processing''       °
  203.  '                                                                             °
  204.   '''of data that is incompatible. This is why the last element of vecto       °
  205.  r'''                                                                          °
  206.   '''NUMBER is a zero. It indicates that the coin collection has no half       °
  207.  ''' '''dollars by the presence of that zero.'',r'                             °
  208.   '''Does this mean that a vector must always be operated on by another'       °
  209.  ''                                                                            °
  210.   '''vector of the same length? Not at all. Suppose that someone were to       °
  211.  ''' '''double the number of coins in the collection:'',r'                     °
  212.   'show ''2⌡NUMBER'''                                                          °
  213.   '''Here we multiplied a scalar (2) by a vector. So for the time being'       °
  214.  '' '''let us write down the following rule:'',2µr' '0 RULE 1'                 °
  215.   '''Later we will see how this rule can be expanded.'''                       °
  216.   '''RULE 1 applies to arithmetic operations. Does this mean that there'       °
  217.  ''                                                                            °
  218.   '''are primitive functions that allow processing of vectors with diffe       °
  219.  rent'''                                                                       °
  220.   '''shapes? Indeed this is the case. We already saw one example:'',r'         °
  221.   'show ''''''ABORIGINE''''ε''''AEIOU'''''''                                   °
  222.   '''In this expression the left argument has shape 9 and the right argu       °
  223.  ment''' '''has the shape 5.'',r'                                              °
  224.   '''Before returning to the coin problem, here are some comments on the       °
  225.  ''' '''rules given in this and in the next few tutorial lessons:'',r'         °
  226. X '''      RULES'',r' 'RULES'                                                  °
  227. *(1997 5 31 11 14 27 420)                                                      °
  228.  FTEACH2 ÉFX 'TEACH2' 'Σ Introduce the reduction operator'                     °
  229.   'exit ''TEACH2'''                                                            °
  230.   '''When we multiply COINS⌡NUMBER, the result is yet another vector.'''       °
  231.   '''but this is not what we want. What we desire is the SUM of the'''         °
  232.   '''individual components of this vector.'',r'                                °
  233.   '''APL2 provides a special symbol (/) to solve this type of a problem.       °
  234.  '''                                                                           °
  235.   '''The slash is neither a primitive function nor a variable. It is'''        °
  236.   '''called a REDUCTION OPERATOR. An OPERATOR is neither a function nor'       °
  237.  ''                                                                            °
  238.   '''a variable. It is a special symbol that tells APL2 what kind of'''        °
  239.   '''process is to be performed. APL2 supports several such OPERATORS.''       °
  240.  ' '''But now let us see how the REDUCTION OPERATOR works:'',r'                °
  241.   'show ''+/COINS⌡NUMBER'''                                                    °
  242.   '''Somehow this expression produced the total amount of money in the''       °
  243.  '                                                                             °
  244.   '''coin collection. We already know that COINS⌡NUMBER produce a vector       °
  245.  :'',r' 'show ''COINS⌡NUMBER''' '''The +/ does the equivalent of:'',r'         °
  246.   'show ''0.03 + 0.25 + 0.2 + 0.75 + 0'''                                      °
  247.   '''(It places the symbol + between all pairs in the vector.)'',r'            °
  248.   '''But why the name REDUCTION? It is called that because for a vector'       °
  249.  '' '''right argument it reduces the result to a scalar.'',r'                  °
  250.   '''Note also that the symbols +/ were placed to the LEFT of COINS⌡NUMB       °
  251.  ER.'''                                                                        °
  252.   '''Since COINS⌡NUMBER must be evaluated first, we MUST conclude that''       °
  253.  ' '''evaluation proceeds from right to left. More on that later.'''           °
  254.   'do' '''Let us tentatively write the following rule:'',2µr'                  °
  255.   '0 RULE 2' 'do' '''Let us see some examples:'',r'                            °
  256.   'show ''+/1 1 1'' ''⌡/1 2 3'' ''-/1 2 3'''                                   °
  257.   '''The last example brings out a new problem. If we begin from left'',       °
  258.  r' 'show ''-/1 2'''                                                           °
  259.   '''and then apply this result to the third element'',r'                      °
  260.   'show ''-/²1 3'''                                                            °
  261.   '''So if we solve 1 - 2 - 3 by evaluating the expression from left to'       °
  262.  ''                                                                            °
  263.   '''right, we get the wrong answer. If we do it from right to left:'',r       °
  264.  ' 'show ''-/2 3'' ''-/1 ²1'''                                                 °
  265.   '''we get the correct answer. So we have yet another rule:'',2µr'            °
  266. X '0 RULE 3' 'endd'                                                            °
  267. *(1997 5 31 11 20 13 388)                                                      °
  268.  FTEACH3 ÉFX 'TEACH3' 'Σ Zeros and nulls' 'exit ''TEACH3'''                    °
  269.   '''Before we leave the reduction operator, we must tackle what may'''        °
  270.   '''appear to be a trivial concept (BUT IS DEFINITELY NOT).'',r'              °
  271.   '''                  ZEROS AND NULLS'',r'                                    °
  272.   '''IF THIS IS THE FIRST TIME YOU READ ABOUT APL2, THIS SECTION MAY SEE       °
  273.  M'''                                                                          °
  274.   '''TOO DIFFICULT. PLEASE, TRY TO FOLLOW THE DISCUSSION, BUT DO NOT'''        °
  275.   '''BE DISCOURAGED IF IT DOES NOT MAKE SENSE TO YOU. THE SUBJECT MAY NO       °
  276.  T''' '''BE FAMILIAR, BUT IT IS IMPORTANT.''' 'do'                             °
  277.   '''In lesson 1 we learned how to use the ∞ (iota) monadic function to'       °
  278.  ''                                                                            °
  279.   '''generate vectors of the ordinal sequence of integers. We also learn       °
  280.  ed'''                                                                         °
  281.   '''to use the monadic function µ (shape) to tell us the length of a'''       °
  282.   '''vector. APL2 was designed to be LOGICALLY, and MATHEMATICALLY true'       °
  283.  ''                                                                            °
  284.   '''no matter what the expressions, as long as these are valid. Let us'       °
  285.  '' '''see where that will lead us:'',r' 'show ''µ5'' ''µ''''X'''''''          °
  286.   '''Since 5 and ''''X'''' are scalars, they have NO SHAPE. This is refl       °
  287.  ected'''                                                                      °
  288.   '''in the fact that the shape of a scalar returns nothing. But nothing       °
  289.  '''                                                                           °
  290.   '''is not a valid datum. There must be some representation of nothing.       °
  291.  ''' 'do'                                                                      °
  292.   '''To see how APL2 resolves this, consider the following sequence of''       °
  293.  ' '''vectors:'',r' 'show ''∞2'' ''µ∞2'' ''∞1'' ''µ∞1'' ''∞0'' ''µ∞0'''        °
  294.   '''Obviously, the index generator function ∞ ALWAYS generates a vector       °
  295.  '''                                                                           °
  296.   '''even for the integer 1. It even generates a vector for the zero!'''       °
  297.   '''So the result of ∞1 is NOT a scalar, but a VECTOR OF LENGTH 1.'''         °
  298.   '''Similarly the result of ∞0 is not nothing, but a VECTOR OF LENGTH''       °
  299.  '                                                                             °
  300.   '''ZERO. Note that the result of ∞0 is the same as the result of µ5.''       °
  301.  ' 'do' '''We can test this hypothesis by'',r' 'show ''µµ5'''                  °
  302.   '''So we have the following rules:'',2µr' '0 RULE 4' '0 RULE 5' 'do'         °
  303.   '''When we wanted to know the shape of the shape of a scalar (µµ5)'''        °
  304.   '''we obtained a zero. The application of one or more primitive'''           °
  305.   '''functions in an APL2 expression has a special name. It is called'''       °
  306.   '''an IDIOM. (∞µ would be an example of another idiom).''' 'do'              °
  307.   '''The µµ idiom is so important that it has its own name. It is called       °
  308.  '''                                                                           °
  309.   '''the RANK. The rank of a scalar is zero, and the rank of a vector is       °
  310.  '''                                                                           °
  311.   '''one. In lesson 1 we used the reshape (dyadic µ) to generate a table       °
  312.  .'''                                                                          °
  313.   '''Such a table has a rank of 2. In a later lesson we will study APL2'       °
  314.  ''                                                                            °
  315.   '''objects of rank greater than TWO. Indeed in APL2OS2 the maximum ran       °
  316.  k''' '''is 64.''' 'do'                                                        °
  317.   '''Before we leave this subject, we should also study how reduction'''       °
  318.   '''works on scalars and vectors of length 0.'',r'                            °
  319.   'show ''+/5'' ''+/∞1'''                                                      °
  320.   '''The reduction of a scalar (or a vector of length one) returns that'       °
  321.  ''                                                                            °
  322.   '''scalar (or vector). But now, since reduction by definition reduces'       °
  323.  ''                                                                            °
  324.   '''the rank by one. (We have not stated this, but recall that the'''         °
  325.   '''reduction of a vector produces a scalar), what should be the shape'       °
  326.  '' '''of +/5? This is handled in APL2 by another rule:'',2µr'                 °
  327.   '0 RULE 6' 'do' '''This rule has some interesting consequences:'''           °
  328.   'show ''+/''''A'''''' ''-/5'' ''ε/0''' 'do'                                  °
  329.   '''Finally, what happens when we reduce a null vector?'',r'                  °
  330.   'show ''+/∞0'' ''⌡/∞0'''                                                     °
  331.   '''We will study this in more detail in another session, but right'''        °
  332.   '''now, note that the result is such that it would have no effect'''         °
  333.   '''for addition in +/∞0, and no effect for multiplication in ⌡/∞0.'''        °
  334.   '''The rule that applies here is one that you may for the time'''            °
  335. X '''being ignore:'',2µr' '0 RULE 7' 'endd'                                    °
  336. *(1997 5 4 12 14 5 228)                                                        °
  337.  FTEACH4 ÉFX 'TEACH4' 'Σ IBM function DISPLAY' 'exit ''TEACH4'''               °
  338.   '''In lesson 1 we alluded to the existence of APL2 programs, but we'''       °
  339.   '''called them USER DEFINED FUNCTIONS. You already have seen some of''       °
  340.  ' '''these (EXIT KEYBOARD). Function EXIT will get you out of the'''          °
  341.   '''lesson, so that you can log off the APL2 session.'',r'                    °
  342.   '''   (Function KEYBOARD is available only in lesson 1).''' 'do'             °
  343.   '''There is another function that you should know about. It is provide       °
  344.  d'''                                                                          °
  345.   '''by IBM, and is called DISPLAY. It is very useful in decribing'''          °
  346.   '''graphically the contents of a variable. Let us look at some APL2'''       °
  347.   '''objects and their shape with DISPLAY:'',r'                                °
  348.   'show ''DISPLAY (∞2) (µ∞2)'' ''DISPLAY (∞1) (µ∞1)'' ''DISPLAY (∞0) (µ∞       °
  349.  0)'''                                                                         °
  350.   '''Note that each example shows a box with two boxes inside. Each'''         °
  351.   '''box has an arrow (╕) in the upper left corner. This means that'''         °
  352.   '''the object in the box is a vector. When a box contains inner boxes,       °
  353.  '''                                                                           °
  354.   '''we talk about NESTED VECTORS. We will study these in future lessons       °
  355.  .''' '''Let us see what scalars look like:'',r'                               °
  356.   'show ''DISPLAY 1 (µ1)'' ''DISPLAY ''''A'''' (µ''''A'''')'''                 °
  357.   '''Note that scalars are not enclosed in a box, but the shape of scala       °
  358.  rs'''                                                                         °
  359.   '''is indeed a vector. The box enclosing µ1 and µ''''A'''' has the Θ s       °
  360.  ymbol on'''                                                                   °
  361.   '''top. This symbol indicates that the object is NULL. We will postpon       °
  362.  e'''                                                                          °
  363.   '''the study of nulls until much later, but meanwhile consider the'''        °
  364.   '''following strange APL2 objects:'',r'                                      °
  365. X 'show ''DISPLAY (µ1) (µ'''' '''') ('''''''') (∞0)''' 'endd'                  °
  366. *(1997 5 4 12 15 44 388)                                                       °
  367.  FTEACH5 ÉFX 'TEACH5' 'Σ Error messages' 'exit ''TEACH5'''                     °
  368.   '''                  ERROR MESSAGES'',r'                                     °
  369.   '''Whenever you enter invalid APL2 expressions, the result is a'''           °
  370.   '''message describing the nature of the error. In this and all'''            °
  371.   '''subsequent lessons, this ERROR MESSAGE is TRAPPED (you will NOT'''        °
  372.   '''see it). Instead you will see the following:'',r'                         °
  373.   '''This is not a valid APL2 expression'',r,''*'',r'                          °
  374.   '''To see the actual error message, you have to enter ? just to the'''       °
  375.   '''right of the *. Go ahead, and try it, enter a nonsense line, and'''       °
  376.   '''then press ENTER. Then enter ? and press ENTER again.''' 'do'             °
  377.   '''In this lesson, nothing else happens. The lesson will continue'''         °
  378.   '''no matter how bad your expression. Outside this lesson, however,'''       °
  379.   '''the error message causes an INTERRUPT. This means that further'''         °
  380.   '''execution has stopped in order to give you a chance to correct'''         °
  381.   '''the error.''' 'do'                                                        °
  382.   '''Typically, the error message comes in three lines. The first line''       °
  383.  '                                                                             °
  384.   '''identifies the error TYPE. The second line reprints the offending''       °
  385.  '                                                                             °
  386.   '''expression. This is needed because in running applications the bad'       °
  387.  ''                                                                            °
  388.   '''line will not be visible. The third line of the error message shows       °
  389.  '''                                                                           °
  390.   '''a caret (^) symbol at the place of the expression where the error''       °
  391.  ' '''was detected.''' 'do'                                                    °
  392.   '''There will be another lesson with more details. Meanwhile, let us''       °
  393.  ' '''generate some typical errors:'',r'                                       °
  394. X 'errors ''ONE'' ''2 3+1 2 3'' ''''''A''''⌡3''' 'endd'                        °
  395. *(1997 5 31 11 21 26 444)                                                      °
  396.  FTEACH6 ÉFX 'TEACH6;VECTOR;A' 'Σ Average of a vector of numbers'              °
  397.   'exit ''TEACH6'''                                                            °
  398.   '''Before we leave this lesson, let us begin solving some real life'''       °
  399.   '''problems. For example, how about writing a formula for calculating'       °
  400.  ''                                                                            °
  401.   '''the average of a sequence of numbers. (We already know what to call       °
  402.  ''' '''that sequence: a vector.)''' 'do'                                      °
  403.   '''The average is obtained by adding all numbers, and by dividing the'       °
  404.  ''                                                                            °
  405.   '''sum by the total number of values. Let us generate a random vector:       °
  406.  '',r' 'show ''VECTOR╜23 64 71 89 34'''                                        °
  407.   '''We know how to add these:'',r' 'show ''+/VECTOR'''                        °
  408.   '''We also know how to count the number of entries in VECTOR:'',r'           °
  409.   'show ''µVECTOR'''                                                           °
  410.   'A╜(«+/VECTOR),''÷'',(«µVECTOR),'' or '',«(+/VECTOR)÷µVECTOR'                °
  411.   '''Let'',q,''s see; the average should be '',A' 'do'                         °
  412.   '''Can we combine these calculations into a single expression? What'''       °
  413.   '''about the following one:'',r' 'show ''+/VECTOR÷µVECTOR'''                 °
  414.   '''It certainly gives the right answer. But let us see how it is'''          °
  415.   '''obtained. We already know that the evaluation goes from right to'''       °
  416.   '''left'',r' 'show ''VECTOR÷µVECTOR'''                                       °
  417.   '''To get this intermediate value, APL2 had to divide EACH element'''        °
  418.   '''of the vector by '',(«µVECTOR),''. This is wasteful of resources.''       °
  419.  ,r'                                                                           °
  420.   '''After all we should get the same answer if we wait to make the'''         °
  421.   '''division until after obtaining the sum (as we did earlier).'''            °
  422.   'do'                                                                         °
  423.   '''There is a way to tell APL2 how to sequence computation, and that''       °
  424.  ' '''is with the usage of parentheses:'',r'                                   °
  425.   'show ''(+/VECTOR)÷µVECTOR'''                                                °
  426.   '''Now proceeding from right to left as before, APL2 obtains µVECTOR,'       °
  427.  ''                                                                            °
  428.   '''but before it can apply this value, it encounters a right parend'''       °
  429.   '''(parend is another word for parenthesis). This stops the normal'''        °
  430.   '''right to left process until after the expression in parends is'''         °
  431.   '''calculated (from right to left too of course). Only then can the'''       °
  432.   '''division proceed.''' 'do'                                                 °
  433.   '''The moral of this is that even though many problems can be written'       °
  434.  ''                                                                            °
  435.   '''in a simple linear way, some thought should always be given to'''         °
  436.   '''the efficiency of the algorithm described by the expression. We hav       °
  437.  e'''                                                                          °
  438.   '''already commented that in APL2 there are many solutions to any one'       °
  439.  ''                                                                            °
  440.   '''problem. Clearly some solutions are better than others. We should''       °
  441.  '                                                                             °
  442.   '''always seek the best ones, even if these are longer or more complex       °
  443.  .''' 'do'                                                                     °
  444.   '''While the primitive functions of APL2 are non-procedural, the proce       °
  445.  dural'''                                                                      °
  446.   '''aspects in problem solving must not be ignored. Therefore:'',2µr'         °
  447. X '0 RULE 8' 'endd'                                                            °
  448. *(1997 8 25 13 13 21 388)                                                      °
  449.  FTEACH7 ÉFX 'TEACH7;VECTOR' 'Σ Summary' 'exit ''TEACH7'''                     °
  450.   '''In lesson 1 you were asked to try out various APL2 expressions'''         °
  451.   '''using the newly defined primitive functions. Did you try to use'''        °
  452.   '''monadic arithmetic functions?''' 'do'                                     °
  453.   'show ''VECTOR╜23 ²33.56 0'' ''+VECTOR'' ''⌡VECTOR'' ''÷VECTOR+0=VECTO       °
  454.  R''' '''Let us take another look at each of these expressions.'',r'           °
  455.   'show ''+VECTOR'''                                                           °
  456.   '''Clearly +VECTOR does not seem to have an effect. It does not for'''       °
  457.   '''numbers that are REAL. (For COMPLEX numbers the monadic plus'''           °
  458.   '''produces the COMPLEX CONJUGATE.) Enter COMPLEX for more on complex'       °
  459.  '' '''numbers.''' 'do'                                                        °
  460.   '''We will give examples of complex number calculations in subsequent'       °
  461.  ''                                                                            °
  462.   '''lessons. You should ignore these if you are not familiar with that'       °
  463.  '' '''subject.''' 'do' 'show ''⌡VECTOR'''                                     °
  464.   '''Clearly the monadic ⌡ does have an effect. For positive numbers it'       °
  465.  ''                                                                            °
  466.   '''produces a 1, it leaves zeros unchanged, and for negative numbers''       °
  467.  '                                                                             °
  468.   '''it produces a ²1. The monadic TIMES is called the SIGNUM. It can be       °
  469.  ''' '''used to return the sign of a numeric variable.''' 'do'                 °
  470.   '''Let us take another look at the monadic divide:'',r'                      °
  471.   'show ''÷VECTOR+0=VECTOR'' ''0=VECTOR'' ''VECTOR+0=VECTOR'''                 °
  472.   '''Why did we have to add 0=VECTOR to VECTOR? Having done that'''            °
  473.   '''is the result correct?''' 'do' '''We can test it with'',r'                °
  474.   'show ''÷÷VECTOR+0=VECTOR'''                                                 °
  475.   '''First, as long as the vector does not contain a zero, the'''              °
  476.   '''monadic divide generates the reciprocals of individual'''                 °
  477.   '''components. Clearly, if an application calls for a reciprocal,'''         °
  478.   '''and there is a possibility that the variable may contain a zero,'''       °
  479.   '''then you can expect an interrupt in the processing. This can be'''        °
  480.   '''avoided, by using a device such as the above expression.''' 'do'          °
  481.   '''But suppose you desire to see a specific number (say 0) when'''           °
  482.   '''taking the reciprocal. See if you can come up with an expression'''       °
  483.   '''that will generate reciprocals, but return 100 in the position'''         °
  484. X '''occupied by zeros. (For one answer, enter RECIPROCAL).''' 'endd'          °
  485. *(1997 4 28 12 47 49 628)                                                      °
  486.  FTEACH8 ÉFX 'TEACH8' 'Σ Roll and deal' 'exit ''TEACH8'''                      °
  487.   '''When we want to generate a vector of numbers, we enter a numeric'''       °
  488.   '''sequence, and press Enter. That takes a lot of keystrokes. Often'''       °
  489.   '''we may not be interested in the actual numbers. In many games,'''         °
  490.   '''for example, all we want is a sequence of random numbers. APL2'''         °
  491.   '''has a special primitive function to do just that. It is called'''         °
  492.   '''ROLL, and the symbol is a question mark'',r'                              °
  493.   'show ''?100'' ''?100'' ''?100'''                                            °
  494.   '''Each time you use roll, you get a random number. The value is an'''       °
  495.   '''integer in the range one up to the value given as argument.'',r'          °
  496.   '''To generate more than one value at a time (i.e. a vector), you'''         °
  497.   '''would enter'',r' 'show ''?100 100 100 100'' ''?4µ100'''                   °
  498.   '''For example, to generate the roll of six dice, you would enter'',r'       °
  499.   'show ''?6µ6'' ''?6µ6'''                                                     °
  500.   '''Notice that some values repeat in the results above. Suppose that''       °
  501.  ' '''in your application you want random values without repetition.'''        °
  502.   '''For that, there is a dyadic version of ? called DEAL:'',r'                °
  503.   'show ''6?6'' ''10?10'''                                                     °
  504.   '''The left argument to deal can be smaller than the right argument'',       °
  505.  r' 'show ''2?6'' ''5?52'''                                                    °
  506.   '''The last example corresponds to the deal of 5 cards from a deck.'''       °
  507.   '''In subsequent lessons we will show how to convert a number in the''       °
  508.  '                                                                             °
  509.   '''range from 1 to 52 to the representation of a playing card such as'       °
  510. X',r' '''        '',ÉAV[4],''K''' '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 4 27 12 39 6 420)                                                       °
  516. XFaq ÉFX 'U╜aq W' 'U╜Γaddquote W'                                              °
  517. *(1997 7 24 13 20 38 476)                                                      °
  518.  Fav ÉFX 'av;A;N;I;ÉIO' 'Σ Display characters in ÉAV' 'ÉIO╜0'                  °
  519.   'A╜22 78µ'' ''' 'N╜3 0«φ12 22µ1+∞356' 'A[;,(6⌡∞12)°.+2 3 4]╜N'               °
  520.   'A[;6+6⌡∞12]╜φ12 22µÉAV' 'ΣA[8 10 13;6]╜'' ''' 'A[13;6]╜'' '''               °
  521. X 'A[14+∞8;68 69 70 72]╜'' ''' 'A'                                             °
  522. *(1991 11 11 8 25 13 316)                                                      °
  523.  Fdate ÉFX 'u╜date w' 'Σ Format date and time of day' 'u╜«6╞w'                 °
  524. X 'u╜('' ''⌠u)Γu' 'u╜εu,■''-- .. '''                                           °
  525. XCdig 1 10 1234567890                                                          °
  526. *(1997 9 9 13 0 45 372)                                                        °
  527.  Fdisclaimer ÉFX 'disclaimer' 'Σ Copyright statement'                          °
  528.   '(10µ'' ''),''Copyright, Z. V. Jizba, 1995,1996,1997'',r'                    °
  529.   ''' This and subsequent workspaces labelled TEACHxx are made available       °
  530.  '''                                                                           °
  531.   '''at no cost to anyone who desires to learn how to use effectively'''       °
  532.   '''the IBM/OS2 version of APL2.'',r'                                         °
  533.   '''This software is provided "AS IS" with no WARRANTY of any kind, eit       °
  534.  her'''                                                                        °
  535.   '''express or implied. Any risk in its use resides with you, the user        °
  536.  of''' '''these tutorials.'',r' '''     ACKNOWLEDGEMENTS'',r'                  °
  537.   ''' In writing these tutorials, I am greatly indebted to Roy Sykes, wh       °
  538.  ose'''                                                                        °
  539.   '''excellent lectures increased my understanding of the language.'''         °
  540.   '''Discussions with the late Harry Bertucelli clarified a number of'''       °
  541.   '''concepts and caused me to change some improper terminology that was       °
  542.  '''                                                                           °
  543.   '''used in previous versions of these tutorials. Mr. Benjamin Archer''       °
  544.  '                                                                             °
  545.   '''kindly checked out a nearly final version, bringing to my attention       °
  546.  ''' '''some ommisions, misspellings, and invalid terminology.'',r'            °
  547. X '''(PRESS ENTER to continue)'''                                              °
  548. *(1997 7 13 12 28 50 508)                                                      °
  549.  Fdo ÉFX 'do;T;E' 'Σ Expression driver' 'E╜''''' 'æ╜''      ''' 'T╜æ'          °
  550.   '╕(^/'' ''=T)/0'                                                             °
  551.   '╕(('':''εT)doif ''B╜evaldd (+/^\'''' ''''=T)╟T'')/2'                        °
  552.   '''E╜ÉEM'' ÉEA T' '╕(0=µ,E)/2'                                               °
  553.   '''This is not a valid APL2 expression''' 'æ╜''*''' '╕(''?''⌠╞1╟æ)/2'        °
  554. X 'E' '╕2'                                                                     °
  555. *(1997 7 13 12 28 50 508)                                                      °
  556.  Fdoif ÉFX 'U╢╜V╢ doif W╢;t╢' 'Σ Rule' '╕(^/~U╢╜V╢)/0'                         °
  557. X '''U╢╜V╢ doif■ W╢'' ÉEA ''»V╢/W╢'''                                          °
  558. *(1997 9 9 12 50 14 444)                                                       °
  559.  Fendd ÉFX 'endd' 'Σ end of special feature' '20µ''²'' ╪ ╕(4<µÉLC)/0'          °
  560. X 'do'                                                                         °
  561. *(1997 8 4 13 31 22 380)                                                       °
  562.  Ferase ÉFX                                                                    °
  563.   'erase;t;EXIT;GO;HELP;DISPLAY;RECIPROCAL;RULE;RULES;DISCLAIMER;COMPLEX       °
  564.  ' 'Σ Erase all global functions and variables' 't╜ÉNL 3'                      °
  565.   't╜(~t^.εlc,'' '')≡t' 't╜ÉEX(~t[;∞5]^.=''TEACH'')≡t' 't╜ÉNL 2'               °
  566. X 't╜ÉEX(~t^.εlc,'' '')≡t' 't╜ÉNL 4' 't╜ÉEX(~t^.εlc,'' '')≡t'                  °
  567. *(1997 4 27 12 39 6 420)                                                       °
  568.  Ferrors ÉFX 'errors W;EC;RT;R' 'Σ Display error message'                      °
  569.   '╕((1<╧W)doif ''errors■ W'')/0' '''       '',W' '(EC RT R)╜ÉEC W' 'R'        °
  570. X 'do'                                                                         °
  571. *(1997 7 27 13 47 41 608)                                                      °
  572.  Fevaldd ÉFX 'u╜evaldd w;c;n' 'Σ Evaluate direct definition' 'u╜0'             °
  573.   'n╜(w∞''Σ'')-1' 'c╜(((n╞w)⌠'':'')Γn╞w),Γ''ΣDD '',(n+1)╟w'                    °
  574.   '╕((1 label╞c)doif ''''''Invalid label'''''')/0'                             °
  575.   '╕((2=µc)doif ''u╜showdd 1╙c'')/0'                                           °
  576.   '╕((3=ÉNC╞c)doif ''u╜⌡µÉ╜(╞c),'''' is already defined.'''''')/0'             °
  577.   '╕((3=µc)doif ''u╜simdd c'')/0' 'c╜(Γ''α∙ aw'')replace■c'                    °
  578.   'u╜ε''u╜'',((''a''εεc[2 3 4])/''a ''),(╞c),'' w;t;b'''                       °
  579.   'u╜u(5πc)(''b╜(t╜'',(3πc),'')/'',addquote ''u╜'',4πc)'                       °
  580. X 'u╜u,''╕(t doif b)/0''(''u╜'',2πc)' 'u╜╧ÉFX u'                               °
  581. *(1997 7 25 13 27 52 564)                                                      °
  582.  Fexit ÉFX 'V exit W;T' 'Σ Exit if too many suspended functions'               °
  583.   '╕(0⌠ÉNC ''V'')/L0 ╪ V╜10' 'L0:╕(V>µÉLC)/0'                                  °
  584.   '''There are too many suspended functions''' '''Please enter '',W'           °
  585. X '╕'                                                                          °
  586. *(1997 7 26 12 33 39 536)                                                      °
  587.  Fget ÉFX 'U╜V get W;t;T;ÉPR' 'Σ Prompt for response from keyboard'            °
  588.   'ÉPR╜T╜ÉAV[ÉIO+255] ╪ ╕(0⌠ÉNC ''V'')/L0 ╪ V╜1' 'L0:V╜V╧1' 'æ╜W ╪ t╜æ'        °
  589.   'U╜(+/^\t=T)╟t' '╕(''╕''⌠╞U)/L1 ╪ ╕' 'L1:╕V/0' 't╜(U⌠'' '')ΓU'               °
  590. X 'U╜(µt),(ΓU),t'                                                              °
  591. *(1997 7 28 13 33 8 424)                                                       °
  592.  Fglobals ÉFX 'globals' 'Σ Initialize useful global variables'                 °
  593.   'uc╜''ABCDEFGHIJKLMNOPQRSTUVWXYZ'''                                          °
  594.   'lc╜''abcdefghijklmnopqrstuvwxyz''' 'dig╜''1234567890'''                     °
  595. X 'r╜ÉAV[13+ÉIO]' 'q╜'''''''''                                                 °
  596. *(1997 7 3 12 47 6 368)                                                        °
  597.  Finitialize ÉFX 'initialize;T' 'Σ Initialize workspace'                       °
  598.   '''AT ALL TIMES, TO CONTINUE, PRESS RETURN!'',r'                             °
  599.   '''To see disclaimers enter:'',r,''    disclaimer''' 'do' 'erase'            °
  600.   'globals'                                                                    °
  601.   '''Make sure the CAP LOCK light on your keyboard (upper right) is ON!'       °
  602. X'' 'endd'                                                                     °
  603. *(1997 7 27 13 14 33 444)                                                      °
  604.  Flabel ÉFX 'u╜v label w'                                                      °
  605.   'Σ Return 1 if label w does not begin with a cap'                            °
  606.   '╕(0⌠ÉNC ''v'')/L0 ╪ v╜0' 'L0:v╜v╧1 ╪ w╜εw ╪ ╕v/L1 ╪ ╕(u╜0⌠ÉNC w)/0'         °
  607. X 'L1:╕(u╜~^/wεlc,uc,dig)/0' 'u╜w[1]εlc,dig'                                   °
  608. XClc 1 26 abcdefghijklmnopqrstuvwxyz                                           °
  609. *(1997 7 13 12 28 55 528)                                                      °
  610.  Fnon ÉFX 'non;T;RC;ET;R' 'Σ Ignore keyboard entry' 'æ╜''      ''' 'T╜æ'       °
  611.   '╕(0=µ(T⌠'' '')/T)/0' '(RC ET R)╜ÉEC T' '╕(0=RC)/2'                          °
  612. X '╕((1=RC)doif ''R'')/2' '╕2'                                                 °
  613. *(1997 7 13 12 28 55 528)                                                      °
  614.  Fnotb ÉFX 'u╜notb w' 'Σ Remove trailing blanks'                               °
  615.   '╕((1<╧w)doif ''u╜notb■ w'')/0' '╕((1<µµw)doif ''u╜πnotb Γ[2]w'')/0'         °
  616. X 'u╜(1-(,'' ''⌠Φw)∞1)╟w'                                                      °
  617. *(1997 4 27 12 39 6 420)                                                       °
  618.  Fpause ÉFX 'V pause W;T'                                                      °
  619.   'Σ Pause, then print W V spaces right and return'                            °
  620. X 'T╜(0=ÉNC ''V'')doif ''V╜6''' 'do' '(Vµ'' ''),W' 'do'                        °
  621. XCq 0 '                                                                        °
  622. XCr 0                                                                         °
  623. *(1997 7 13 12 28 56 532)                                                      °
  624.  Freplace ÉFX 'u╜v replace u;i;r;s' 'Σ Replace elements in v in u'             °
  625.   'i╜Γ∞µu' 's╜2πv╜(v⌠'' '')Γv' 'i╜⌡r╜i⌡■Γ[1]u°.=╞v'                            °
  626. X 'u[(εi)/εr]╜s[(εi)/εi⌡■∞µs]'                                                 °
  627. *(1997 7 13 12 28 56 532)                                                      °
  628.  Fround ÉFX 'U╜V round W' 'Σ Half adjust to V th decimal'                      °
  629. X 'U╜(╛0.5+W⌡10*V)÷10*V'                                                       °
  630. *(1997 8 5 12 48 37 508)                                                       °
  631.  Frule ÉFX 'V rule W' 'Σ Additional remarks on rules'                          °
  632.   '╕(2=ÉNC ''V'')/L ╪ V╜0' 'L:╕((╞W)=∞8)/L1,L2,L3,L4,L5,L6,L7,L8'              °
  633.   '''For rules not described in this lesson, see workspace INDEX'''            °
  634.   '╕0' 'L1:╕(V>1)/L1X'                                                         °
  635.   '''RULE 1. For arithmetic operations a vector must be matched with a''       °
  636.  ' '''        scalar, or another vector of the same length.'',2µr'             °
  637.   '╕(0=V)/0'                                                                   °
  638.   'L1X:''This is the first of many rules dealing with the concept of'''        °
  639.   '''CONFORMABILITY. This means that elements to be processed by APL2'''       °
  640.   '''must in some ways match.''' '╕L0' 'L2:╕(V>1)/L2X'                         °
  641.   '''RULE 2. The reduction operator places the function, (specified to''       °
  642.  '                                                                             °
  643.   '''        its left), between all pairs of the vector. It then evaluat       °
  644.  es''' '''        the resulting expression.'',2µr' '╕(0=V)/0'                  °
  645.   'L2X:''It is apropriate to think of an operator as a device to generat       °
  646.  e''' '''derived functions.''' '╕L0' 'L3:╕(V>1)/L3X'                           °
  647.   '''RULE 3. All APL2 expressions are evaluated from right to left'',2µr       °
  648.  ' '╕(0=V)/0'                                                                  °
  649.   'L3X:''This rule is valid in APL, which is the earlier version of APL2       °
  650.  .'''                                                                          °
  651.   '''For APL2, the rule has been restated in a totally different way.'''       °
  652.   '''However until we introduce some concepts that are new to APL2, the'       °
  653.  '' '''newer form reduces to RULE 3 as stated.''' '╕L0' 'L4:╕(V>1)/L4X'        °
  654.   '''RULE 4. Vectors in APL2 can have lengths of zero and one.'',2µr'          °
  655.   '╕(0=V)/0'                                                                   °
  656.   'L4X:''This is the first concept that begins to introduce the idea tha       °
  657.  t no'''                                                                       °
  658.   '''matter what the APL expression, if it is valid, it must produce a''       °
  659.  '                                                                             °
  660.   '''valid APL2 object. (Shades of which came first, the chicken or the        °
  661.  egg)''' '╕L0' 'L5:╕(V>1)/L5X'                                                 °
  662.   '''RULE 5. The shape of a variable is a vector. The shape of a vector'       °
  663.  '' '''        is a vector of length 1. The shape of a scalar is a'''          °
  664.   '''        vector of length zero.'',2µr' '╕(0=V)/0'                          °
  665.   'L5X:''The left argument of RESHAPE is by definition a vector. If APL2       °
  666.   is'''                                                                        °
  667.   '''to be internally consistent, the inverse operation to RESHAPE (whic       °
  668.  h''' '''is indeed SHAPE) MUST return the same type of object.''' '╕L0'        °
  669.   'L6:╕(V>1)/L6X'                                                              °
  670.   '''RULE 6. Reduction of a scalar returns that scalar REGARDLESS of'''        °
  671.   '''        the function to the left of the reduction operator.'',2µr'        °
  672.   '╕(0=V)/0'                                                                   °
  673.   'L6X:''Reduction BY DEFINITION reduces the RANK by one. The exception        °
  674.  is'''                                                                         °
  675.   '''for objects whose rank is zero. Since there is no such thing as'''        °
  676.   '''negative rank, reduction is undefined for scalars, and therefore'''       °
  677.   '''the result is to leave the APL2 object unchanged.''' '╕L0'                °
  678.   'L7:╕(V>1)/L7X'                                                              °
  679.   '''RULE 7. The reduction of a null vector produces the identity'''           °
  680.   '''        element of the function.'',2µr' '╕(0=V)/0'                        °
  681.   'L7X:''A null vector may be empty, but it still has rank equal to one.       °
  682.  '''                                                                           °
  683.   '''Therefore its reduction MUST be a scalar. Since there is no data'''       °
  684.   '''in a null vector, the scalar must be such that it has no effect'''        °
  685.   '''on the operation performed by the function: Zero added to anything'       °
  686.  ''                                                                            °
  687.   '''has no effect in addition; one multiplied by anything has no effect       °
  688.  '''                                                                           °
  689.   '''in multiplication; and so on. (Mathematicians call any such value''       °
  690.  '                                                                             °
  691.   '''that has NO effect on the operation of a function "The identity'''        °
  692.   '''element".)''' '╕L0' 'L8:╕(V>1)/L8X'                                       °
  693.   '''RULE 8. Parentheses can (and should) be used to modify the right'''       °
  694.   '''        to left rule to improve efficiency'',2µr' '╕(0=V)/0'              °
  695.   'L8X:''Parentheses are used in APL2 for other purposes as well, but fo       °
  696.  r'''                                                                          °
  697.   '''now, we will use them just to modify the right to left rule (RULE 3       °
  698.  ).'''                                                                         °
  699.   '''If you place parentheses where they are not needed, APL2 will ignor       °
  700. Xe''' '''them.''' 'L0:''²²²'',r' '╕(0=µW╜1╟W)/0' '╕L'                          °
  701. *(1997 7 13 12 28 57 536)                                                      °
  702.  Fshow ÉFX '╢V show ╢W;╢T;╢B' 'Σ Display and execute ╢W'                       °
  703.   '╢T╜(0=ÉNC ''╢V'')doif ''╢V╜0'''                                             °
  704.   '╕((0=╧╢W)doif ''show ╢W,'''' '''''')/0'                                     °
  705.   '╕((1<╧╢W)doif ''╢V show ■╢W'')/0' '''      '',╢W'                           °
  706. X '╕((╢V^'':''ε╢W)doif ''╢T╜evaldd ╢W'')/L0' '''ÉEM'' ÉEA ╢W' 'L0:do'          °
  707. *(1997 7 13 12 28 57 536)                                                      °
  708.  Fshowdd ÉFX 'u╜showdd w;a;b;c;r'                                              °
  709.   'Σ Display a direct definition function'                                     °
  710.   '╕((1=╧w)doif ''u╜showdd Γw'')/u╜0'                                          °
  711.   '╕((3⌠ÉNC╞w)doif ''(ε╞w),'''' is not a function'''''')/0'                    °
  712.   'c╜Γ[2]ÉCR╞w' 'c╜notb(2╞c),(Γ''aw α∙'')replace■2╟c'                          °
  713.   '╕((~''ΣDD''╧3╞2πc)doif ''''''Not a direct definition function'''''')/       °
  714.  0' 'u╜1' 'b╜('' ''⌠╞c)Γ╞c' 'a╜''      ''' 'r╜2╟3πc'                           °
  715.   '╕((3=µc)doif ''a,(╞w),'''':'''',r,(3<µ2πc)/''''   Σ'''',3╟2πc'')/0'         °
  716.   'a╜a,(╞w),'':'',(2╟5πc),'':''' 'b╜(+\r=''('')-+\r='')''' 'b╜b∞0'             °
  717. X 'a╜a,(²3╟(b-1)╞3╟r),'':'',2╟»(b+2)╟r' 'a,(3<µ2πc)/''  Σ'',3╟2πc'             °
  718. *(1997 7 13 12 28 57 536)                                                      °
  719.  Fshowfn ÉFX 'U╜V showfn W;F;N;T;ÉIO' 'Σ Simulate ╖W[É]'                       °
  720.   'T╜(0=ÉNC ''V'')doif ''V╜0''' 'ÉIO╜0'                                        °
  721.   'U╜r,''      '',''╖'',W,''[É]'',(╞V)╞''╖''' 'N╜1╞µF╜ÉCR W' 'N╜«∞N'           °
  722.   'N╜(N⌠'' '')ΓN' 'F╜(π''['',■N,■Γ''] ''),F'                                   °
  723.   'T╜(1<µ,V)doif ''F╜F[1╟V;]'' ''U╜''''''''''' 'U╜²1╟U,r,,F,r'                 °
  724. X 'U╜((-+/^\'' ''=ΦU)╟U),('' ╖'')[╞V],r'                                       °
  725. *(1997 7 13 12 28 58 540)                                                      °
  726.  Fsimdd ÉFX 'u╜simdd w;e' 'Σ Direct definition mode' 'u╜0'                     °
  727.   '╕((0⌠ÉNC╞w)doif ''''''Already defined'''''')/0' 'e╜''α''ε2πw'               °
  728.   'w[2]╜Γ''u╜'',''α∙ aw'' replace 2πw' 'w╜w[1 3 2]'                            °
  729. X 'w[1]╜Γε''u╜'',(e/''a ''),w[1],'' w''' 'u╜╧ÉFX w'                            °
  730. *(1992 6 3 9 59 17 424)                                                        °
  731.  Ftab ÉFX 'U╜V tab W;T;A;B;C;D;E;F;G;M;ÉPW' 'Σ Tabulate list W'                °
  732.   'T╜(0=ÉNC ''V'')doif ''V╜0''' 'M╜''Invalid data for tabulation'''            °
  733.   'V╜4╞V' 'ÉPW╜130╛30⌐G╜V[2]+79⌡V[2]=0'                                        °
  734.   'L1:╕((1<╧W)doif ''''''W╜∞0'''' ÉEA ''''W╜πW'''''')/L1'                      °
  735.   '╕(((0=µεW)δ2<µµW)doif ''U╜(~V╧4╞0)/M'')/0'                                  °
  736.   'T╜(1≥µµU╜«W)doif ''U╜πW╜(U⌠'''' '''')ΓU'''                                  °
  737.   'T╜(0<V[1])doif ''U╜(«(Φ1,╞µW)µ(V[3]µ'''' ''''),∞(╞µW)-V[3]),'''' ''''       °
  738.  ,U''' '╕(G<30)/0' 'T╜(F╜µεV[4])+C╜1╟B╜µA╜(V[3],0)╟U'                          °
  739.   'T╜⌐(1╞B)÷1⌐╛(ÉPW+F)÷T' 'U╜(E╜(V[3],C)╞U),[1](B╜T,1╟B)╞A'                    °
  740.   '''D╜εV[4]'' ÉEA ''D╜ÉAV[εV[4]+33⌡V[4]=0]''' 'L0:A╜(T,0)╟A'                  °
  741. X '╕(0=1╞µA)/0' 'U╜U,(((T+V[3]),µD)µD),E,[1]B╞A' '╕L0'                         °
  742. *(1997 7 13 12 28 59 544)                                                      °
  743.  Ftest ÉFX 'U╜V test W;P'                                                      °
  744.   'Σ Describe problem in W, (correct answer in V)' 'U╜2' 'L1:W'                °
  745.   'É╜''      ''' 'P╜æ' '''╕L0'' ÉEA ''P╜»P''' '╕(V╧P)/0'                       °
  746. X 'L0:╕(0=U╜U-1)/0' '''Incorrect. Try again''' '╕L1'                           °
  747. XCuc 1 26 ABCDEFGHIJKLMNOPQRSTUVWXYZ                                           °
  748.