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

  1. XNÉIO 0 1                                                                      °
  2. XNÉCT 0 1E²13                                                                  °
  3. XCÉFC 1 6 .,*0_²                                                               °
  4. XNÉRL 0 1847012583                                                             °
  5. XCÉPR 1 1                                                                      °
  6. XCÉLX 1 5 TEACH                                                                °
  7.  AANTE╜('1')('21=ISIN R╜EVAL P')('21<ISIN R╜EVAL P')('14 22<ISIN R')('1'       °
  8. X)                                                                             °
  9. *(1996 4 6 16 7 36 340)                                                        °
  10.  FBIND ÉFX 'BIND' 'Σ Binding rules of APL2'                                    °
  11.   '''  1. Brackets bind to the left'''                                         °
  12.   '''  2. Left arrow binds to the left'''                                      °
  13.   '''  3. Dyadic operator binds to the right'''                                °
  14.   '''  4. Strand notation binding'''                                           °
  15.   '''  5. Operator binds to its left'''                                        °
  16.   '''  6. Function binds to its left'''                                        °
  17.   '''  7. Function binds to its right'''                                       °
  18. X '''  8. Left arrow binds to its right'',r'                                   °
  19. *(1997 8 17 11 44 17 456)                                                      °
  20.  FBLACKJACK ÉFX 'BLACKJACK;D;DECK;T;S;R'                                       °
  21.   'Σ Command for game of Blackjack' 'DECK╜∞52 ╪ DECK╜DECK[52?52]'              °
  22.   'D╜''Player'' ''I win'' ''I fold'' ''Hit me'' ''I stand'' ''BLACKJACK'       °
  23.  '' '╕(INFEN D)/0 ╪ T╜æ ╪ ╕(21<S╜R)/0'                                         °
  24.   'D╜(5╞''Dealer'' ''You lose'' ''You win''),5╟D ╪ ╕(INFEN D)/0'               °
  25. X '╕(S≥R)/L0 ╪ D[2] ╪ ╕0' 'L0:D[3]'                                            °
  26. *(1997 8 17 11 44 21 472)                                                      °
  27.  FBLACKJACK2 ÉFX 'BLACKJACK2;D;DECK;T;S;R'                                     °
  28.   'Σ Command for game of Blackjack'                                            °
  29.   'DECK╜∞52 ╪ DECK╜DECK[52?52] ╪ P╜DEAL 2'                                     °
  30.   'D╜''Player'' ''I win'' ''I fold'' ''Hit me'' ''I stand'' ''BLACKJACK'       °
  31.  '' 'INFEN2 RULES ╪ T╜æ ╪ ╕(21<S╜R)/0'                                         °
  32.   'D╜(5╞''Dealer'' ''You lose'' ''You win''),5╟D ╪ INFEN2 RULES'               °
  33. X '╕(S≥R)/L0 ╪ D[2] ╪ ╕0' 'L0:D[3]'                                            °
  34.  ACONS╜('D[1] ''gets'' 2 ''cards:'' ╪ SHOW P╜DEAL 2 ╪ T╜æ')('D[6 2] ╪ N╜       °
  35.  ²1')('D[3] ╪ N╜²1')('D[5] ╪ N╜²1')('D[4] ╪ SHOW P╜P,DEAL 1 ╪ T╜æ ╪ N╜2'       °
  36. X)                                                                             °
  37. *(1997 8 17 11 43 58 616)                                                      °
  38. XFDEAL ÉFX 'U╜DEAL W' 'Σ Deal W cards' 'U╜W╞DECK' 'DECK╜W╟DECK'                °
  39.  NDECK 1 50 13 47 6 26 21 16 2 41 29 38 52 10 40 22 4 51 33 23 30 49 3         °
  40.   14 50 45 18 37 28 8 12 36 42 11 19 44 7 31 27 24 39 1 35 17 48 25 15         °
  41. X 20 5 32 9 43                                                                 °
  42. *(1997 9 14 12 30 44 504)                                                      °
  43. XFDISCLAIMER ÉFX 'DISCLAIMER' 'Σ Copyright statement' 'disclaimer'             °
  44. *(1996 4 6 16 7 36 340)                                                        °
  45.  FDISPLAY ÉFX 'D╜S DISPLAY A;ÉIO;R;C;HL;HC;HT;HB;VL;VB;V;W;N;B'                °
  46.   'Σ DISPLAY A GENERAL ARRAY IN PICTORIAL FORM'                                °
  47.   'Σ  NORMAL CALL IS MONADIC.  DYADIC CALL USED ONLY IN'                       °
  48.   'Σ    RECURSION TO SPECIFY DISPLAY RANK, SHAPE, AND DEPTH.' 'ÉIO╜0'          °
  49.   '»(0=ÉNC ''S'')/''S╜µA''' 'R╜╞µ,S                   Σ PSEUDO RANK.'          °
  50.   'C╜''┐┌└┘''                 Σ UR, UL, LL, AND LR CORNERS.'                   °
  51.   'HL╜''─''                   Σ HORIZONTAL LINE.'                              °
  52.   'HC╜HL,''Θ╕'',HL,''~+ε''      Σ HORIZONTAL BORDERS.'                         °
  53.   'HT╜HC[(0<R)⌡1+0<╞²1╞,S]' 'ΣW╜,0╧■╞0µΓ(1⌐µA)╞A'                              °
  54.   'HB╜HC[3+3╛(''2⌡~A╧«A'' ÉEA ''1+╞ε0⌡(1⌐⌡/µA)╞,A'')+3⌡1<µµS]'                 °
  55.   'VL╜''│''                   Σ VERTICAL LINE.'                                °
  56.   'VB╜VL,''Φ╟''               Σ VERTICAL BORDER.'                              °
  57.   'V╜VB[(1<R)⌡1+0<²1╞²1╟,S]'                                                   °
  58.   '»(0εµA)/''A╜(1⌐µA)µΓ╞A''   Σ SHOW PROTOTYPE OF EMPTIES.'                    °
  59.   '╕(1<╧A)/GEN' '╕(2<µµA)/D3'                                                  °
  60.   'D╜«A                     Σ SIMPLE ARRAYS.' 'W╜1╞µD╜(²2╞1 1,µD)µD'           °
  61.   'N╜²1+1╟µD' '╕(0=µµA)/SS'                                                    °
  62.   'D╜(C[1],V,((W-1)µVL),C[2]),((HT,NµHL),[0]D,[0]HB,NµHL),C[0],(WµVL),C[       °
  63.  3]' '╕0' 'SS:HB╜((0 '' '')=╞0µΓA)/'' -'''                                     °
  64.   'D╜'' '',('' '',[0]D,[0]HB,Nµ'' ''),'' ''' '╕0'                              °
  65.   'GEN:D╜«DISPLAY■A          Σ ENCLOSED ...' 'N╜Dδ.⌠'' '''                     °
  66.   'D╜(Nδ~1ΦN)≡D' 'D╜(δ≡~''  ''╤D)/D' 'D╜((1,µS)µS)DISPLAY D'                   °
  67.   '╕(2≥µ,S)╟D3E,0' 'D3:D╜0 ²1╟0 1╟«ΓA         Σ MULT-DIMENSIONAL ...'          °
  68.   'W╜1╞µD' 'N╜²1+1╟µD'                                                         °
  69.   'D╜(C[1],V,((W-1)µVL),C[2]),((HT,NµHL),[0]D,[0]HB,NµHL),C[0],(WµVL),C[       °
  70.  3]' 'D3E:N╜²2+µ,S'                                                            °
  71. X 'V╜C[Nµ1],[0]VB[1+0<²2╟,S],[0](((²3+╞µD),N)µVL),[0]C[Nµ2]' 'D╜V,D'           °
  72. *(1997 8 17 11 44 1 392)                                                       °
  73.  FEVAL ÉFX 'U╜EVAL W;R' 'Σ Evaluate a blackjack hand'                          °
  74. X 'U╜+/((∞10),3µ10)[1+R╜13|W]' '╕(~0εR)/0 ╪ U╜0¥U,(22>U+10)/U+10'              °
  75. *(1996 4 6 16 7 36 340)                                                        °
  76.  FEXIT ÉFX 'EXIT' 'Σ Exit from function' '''To log off type:   )OFF'''         °
  77. X '╕'                                                                          °
  78. *(1997 8 17 11 42 34 516)                                                      °
  79.  FGETNO ÉFX 'U╜V GETNO W;E' 'Σ Prompt for numeric data'                        °
  80.   'W ╪ U╜É ╪ ╕(0=ÉNC ''V'')/0' 'L0:╕((╞V)≤µ,U)/L1 ╪ U╜U,É ╪ ╕L0'               °
  81.   'L1:U╜(╞V)╞U ╪ ╕(1=µ,V)/0 ╪ U╜(╛0.5+U⌡10*V[2])÷10*V[2]'                      °
  82.   '╕(2=µV)/0 ╪ V╜(4⌐µV)╞V' 'E╜(U>V[3])-U<V[4] ╪ ╕(^/E=0)/0'                    °
  83. X 'V╜6╞V,V[3 4] ╪ U[(E=1)/∞µE]╜V[5] ╪ U[(E=²1)/∞µE]╜V[6]'                      °
  84. *(1996 4 6 16 7 36 340)                                                        °
  85.  FGO ÉFX 'GO;T;E;B' 'Σ Expression driver' 'L0:B╜E╜''''' 'æ╜''      '''         °
  86.   'T╜æ' '╕(^/'' ''=T)/L0' '╕((^/'')OFF ''=5╞6╟T)doif ''EXIT'')/0'              °
  87.   '╕(('':''εT)doif ''B╜evaldd (+/^\'''' ''''=T)╟T'')/L0'                       °
  88.   '''E╜ÉEM'' ÉEA T' '╕(0=µ,E)/L0' '╕B/L0'                                      °
  89.   '''This is not a valid APL2 expression''' 'æ╜''*'''                          °
  90. X '╕(''?''⌠╞1╟æ)/L0' 'E' '╕L0'                                                 °
  91. *(1997 8 17 11 40 13 424)                                                      °
  92.  FHELP ÉFX 'HELP;N;I;T' 'Σ Help to student' '''WHAT TO DO'',r'                 °
  93.   '''  ° To get out of the lesson''' '''       ENTER: EXIT'''                  °
  94.   '''  ° To log off APL2'''                                                    °
  95.   '''       FIRST, ENTER: EXIT  THEN ENTER: )OFF'''                            °
  96.   '''  ° To get help''' '''       ENTER: HELP'''                               °
  97.   '''  ° When you see the prompt on a blank line'''                            °
  98.   '''       ENTER AN APL2 EXPRESSION - OR JUST PRESS: ENTER''' 'do'            °
  99.   '''  ° If you get this line'',r'                                             °
  100.   '''This is not a valid APL2 expression'',r,''*'',r'                          °
  101.   '''        YOU CAN EITHER''' '''           A. PRESS: ENTER'''                °
  102.   '''           B. PRESS: ? and then ENTER to see what was incorrect'''        °
  103.   '''              in your expression causing that response'',r' 'do'          °
  104.   '''HINTS'',r'                                                                °
  105.   '''This lesson is made up of '',(«N╜7),'' components named TEACHx, whe       °
  106.  re''' '''the x stands for a digit:'',r' 'I╜0'                                 °
  107.   'L0:T╜''TEACH'',(«I╜I+1)' 'T,'' '',1╟notb(ÉCR T)[2;]' '╕(N>I)/L0'            °
  108.   'do'                                                                         °
  109.   '''You may review either of these components separately. To do that,''       °
  110.  '                                                                             °
  111.   '''first enter EXIT (and RETURN), then enter the name of the lesson.''       °
  112.  ' '''component (e.g. TEACH4).'',r'                                            °
  113.   '''To re-start the lesson, just enter TEACH'',r'                             °
  114.   '''When the screen fills up, it is a good idea to move the cursor to''       °
  115.  ' '''the start of a convenient paragraph, and press ENTER'',r'                °
  116.   '''You may also wish to press PAGE UP to review the prevous pages.'''        °
  117.   'do' '''RESOURCES'',r'                                                       °
  118.   '''You may also enter ONE of the following words at a time'',r'              °
  119.   '''BIND       List binding rules'''                                          °
  120.   '''BLACKJACK  Sample blackjack game'''                                       °
  121. X '''REVIEW     Review the contents of the previous lesson''' '' 'endd'        °
  122. *(1997 8 17 11 44 14 444)                                                      °
  123.  FINFEN ÉFX 'U╜INFEN W;P;T' 'Σ Blackjack rules and logic' 'U╜0'                °
  124.   'W[1],''gets'' 2 ''cards:'' ╪ SHOW P╜DEAL 2'                                 °
  125.   'L0:╕(21⌠ISIN R╜EVAL P)/L1 ╪ W[6 2] ╪ U╜1 ╪ ╕0'                              °
  126.   'L1:╕(21>ISIN R)/L2 ╪ U╜1 ╪ W[3] ╪ ╕0'                                       °
  127.   'L2:╕(~14 21<ISIN R)/L3 ╪ ΓW[5] ╪ ╕0'                                        °
  128. X 'L3:W[4] ╪ T╜æ ╪ SHOW P╜P,DEAL 1 ╪ ╕L0'                                      °
  129. *(1997 8 17 11 44 21 472)                                                      °
  130.  FINFEN2 ÉFX 'INFEN2 W;N;ANTE;CONS' 'Σ A simple Inference Engine'              °
  131.   '(ANTE CONS)╜W ╪ N╜0'                                                        °
  132. X 'L0:╕(²1=N)/0 ╪ N╜N+1 ╪ ╕(~U╜0¥»NπANTE)/L0 ╪ »NπCONS ╪ ╕L0'                  °
  133. *(1997 8 17 11 44 3 400)                                                       °
  134.  FISIN ÉFX 'U╜V(f ISIN)W' 'Σ Do cards W add up to range V' 'U╜(0¥ΦV)f W'       °
  135. X '╕(1=µ,V)/0' 'U╜U^W f V[2]'                                                  °
  136. XNNO 0 97                                                                      °
  137. XNR 0 17                                                                       °
  138. *(1996 4 6 16 7 36 340)                                                        °
  139.  FREVIEW ÉFX 'REVIEW;OP' 'Σ Review outer product and defined operator'         °
  140.   '''In lesson 12 we inroduced a one function version of the dot operato       °
  141.  r''' '''called the outer product'',r'                                         °
  142.   'show ''(∞5)°.-∞5'' ''(∞3)°.Φ''''ABC'''' ''''CDE'''' ''''EFG'''''''          °
  143.   '''When the left operand of the dot is the symbol JOT (°), the result'       °
  144.  ''                                                                            °
  145.   '''is an array with dimensions equal to the sum of dimensions of its''       °
  146.  ' '''arguments. The elements of this result include all possible'''           °
  147.   '''compositions of the two arguments'',r'                                    °
  148.   'show ''µ(2 3µ∞6)°.⌡4 5µ∞20'''                                               °
  149.   '''We also studied the user defined operator. This object is distingui       °
  150.  shed'''                                                                       °
  151.   '''from a user defined function by the presence of parentheses in the'       °
  152.  '' '''header'',r'                                                             °
  153.   'show ''ÉFX ''''(f OP) W'''' ''''W°.f W'''''' ''⌡OP ∞4'''                    °
  154.   '''A defined operator is useful in situations where the algorithm is''       °
  155.  '                                                                             °
  156.   '''independent of the function to be used. The assignment of the funct       °
  157.  ion'''                                                                        °
  158.   '''to be used (called binding) can be made just prior to the execution       °
  159.  '''                                                                           °
  160.   '''of the function. The operand(s) in a user defined operator can be''       °
  161.  ' '''user defined function(s).'',r'                                           °
  162.   'show ''ÉFX ''''U╜ODDS W'''' ''''U╜²1+2⌡∞W'''''' ''⌡OP ODDS 4'''             °
  163. X 'endd'                                                                       °
  164.  ARULES╜(('1')('21=ISIN R╜EVAL P')('21<ISIN R╜EVAL P')('14 22<ISIN R')('       °
  165.  1'))(('D[1] ''gets'' 2 ''cards:'' ╪ SHOW P╜DEAL 2 ╪ T╜æ')('D[6 2] ╪ N╜²       °
  166.  1')('D[3] ╪ N╜²1')('D[5] ╪ N╜²1')('D[4] ╪ SHOW P╜P,DEAL 1 ╪ T╜æ ╪ N╜2')       °
  167. X)                                                                             °
  168. *(1997 8 17 11 43 56 608)                                                      °
  169.  FSHOW ÉFX 'U╜SHOW C' 'Σ Display cards C'                                      °
  170. X 'U╜(3Φ''JQKA23456789'',Γ''10'')[1+13|C]' 'U╜U,■(ÉAV[6 5 4 7])[⌐C÷13]'        °
  171. *(1997 6 30 20 58 36 668)                                                      °
  172.  FTEACH ÉFX 'TEACH' 'Σ Start lesson #13: APL2 by Zdenek V JIZBA'               °
  173.   'exit ''TEACH''' 'initialize' 'TEACH1 Σ The quad and the quote quad'         °
  174.   'TEACH2 Σ Prompting; the function GETNO'                                     °
  175.   'TEACH3 Σ The execute primitive and the monadic format'                      °
  176.   'TEACH4 Σ Rndom link and time stamp'                                         °
  177.   'TEACH5 Σ Shuffling, displaying and dealing cards'                           °
  178.   'TEACH6 Σ Blackjack as a production system'                                  °
  179. X 'TEACH7 Σ Blackjack as a rule based system' 'problems'                       °
  180. *(1997 6 22 12 33 7 388)                                                       °
  181.  FTEACH1 ÉFX 'TEACH1;NO;CH;FOO;T;ÉPR' 'Σ Quad and quote quad input'            °
  182.   'ÉPR╜'' '''                                                                  °
  183.   '''To refresh your memory about the previous lesson, enter REVIEW.'''        °
  184.   'do'                                                                         °
  185.   '''In this lessons we will study new primitive functions related to'''       °
  186.   '''the process of interfacing a workspace with external devices. We wi       °
  187.  ll''' '''also discuss utilities and programming strategies.''' 'do'           °
  188.   '''When you enter an expression at the keyboard, you type a string of'       °
  189.  ''                                                                            °
  190.   '''characters. APL2 recognizes the presence or absence of leading and'       °
  191.  ''                                                                            °
  192.   '''trailing quotes in this string. If the quotes are present, the stri       °
  193.  ng'''                                                                         °
  194.   '''is returned to the screen without the quotes. If the quotes are abs       °
  195.  ent,'''                                                                       °
  196.   '''APL2 EVALUATES the string, and if it is a valid APL2 expression, it       °
  197.  ''' '''will complete the evaluation.''' 'do'                                  °
  198.   '''     THE QUAD AND THE QUOTE QUAD'',r'                                     °
  199.   '''When APL2 is requested to display data on the screen, these are alw       °
  200.  ays'''                                                                        °
  201.   '''converted to a visual character representation. (Numeric data are N       °
  202.  OT'''                                                                         °
  203.   '''stored internally as decimal symbols.) We will now study APL2 primi       °
  204.  tive'''                                                                       °
  205.   '''functions that support communication between the workspace memory,        °
  206.  the'''                                                                        °
  207.   '''keyboard, the screen and other I/O (Input/Output) devices. We will        °
  208.  also'''                                                                       °
  209.   '''study APL2 primitive functions to translate between character strin       °
  210.  gs''' '''and numeric arrays.''' 'do'                                          °
  211.   '''The contents of character strings can be data or APL2 expressions.'       °
  212.  ''                                                                            °
  213.   '''Data can be character or numeric. We must be able to specify to APL       °
  214.  2'''                                                                          °
  215.   '''The nature of data that we enter at the keyboard. Here is an exampl       °
  216.  e.'',r'                                                                       °
  217.   '''We know how to generate numeric or character data strings'',r'            °
  218.   'show ''NO╜1 (2 3) (3 3µ∞9)'' ''CH╜''''(∞4)°.⌡∞5'''''''                      °
  219.   '''The first expression generates a numeric nested vector. The second'       °
  220.  ''                                                                            °
  221.   '''expression generates a character string that also happens to be a''       °
  222.  '                                                                             °
  223.   '''valid APL2 expression. Suppose now, that the contents of NO and CH'       °
  224.  ''                                                                            °
  225.   '''is to be determined at the keyboard by the user. APL2 has two basic       °
  226.  '''                                                                           °
  227.   '''keyboard entry primitive functions called QUAD and QUOTE QUAD. The'       °
  228.  ''                                                                            °
  229.   '''quad (É) generates a special prompt (É:) indicating that only numer       °
  230.  ic'''                                                                         °
  231.   '''data is to be entered. (This includes any APL2 expression that'''         °
  232.   '''evaluates to a numeric array). In the following example you MUST'''       °
  233.   '''enter some numbers'',r' 'show ''NO╜É'' ''2⌡NO'''                          °
  234.   '''The quote quad places the cursor at the beginning of the line, but'       °
  235.  ''                                                                            °
  236.   '''does not display a prompt. Anything you enter on that line prior to       °
  237.  '''                                                                           °
  238.   '''ENTER becomes part of the string. In the next example, when you see       °
  239.  '''                                                                           °
  240.   '''the prompt, first enter some blanks, then some characters and a few       °
  241.  ''' '''more blanks before pressing ENTER:'',r' 'CH╜'''''                      °
  242.   'show ''CH╜æ'' ''DISPLAY CH'''                                               °
  243.   '''If you did as instructed, you will see that all blanks have been st       °
  244.  ored''' '''in CH.'',r' 'do'                                                   °
  245.   '''The quad and quote quad have yet another function. When a complex''       °
  246.  '                                                                             °
  247.   '''APL2 expression contains intermediate results, we can display these       °
  248.  ''' '''by assigning them to the quad'',r' 'show ''+/É╜3 3µ∞9'''               °
  249.   '''Without the É╜ we would see only the final result. The 3 by 3 array       °
  250.  ''' '''is displayed in a distinct color, but not stored.'',r' 'do'            °
  251.   '''Assignment to the quote quad is best illustrated with an example'',       °
  252.  r' 'T╜ÉFX ''U╜FOO W'' ''æ╜W'' ''U╜æ''' '1 showfn ''FOO'''                     °
  253.   '''Note that ÉPR is local to function FOO.'',r'                              °
  254.   'show ''CH╜FOO ''''Enter anything: '''''' ''DISPLAY CH'''                    °
  255.   '''In function FOO, the right argument is the prompt. On line [2] this       °
  256.  '''                                                                           °
  257.   '''prompt is assigned to the quote quad. This causes the prompt to be'       °
  258.  ''                                                                            °
  259.   '''displayed on the screen and to place the cursor at the end of the p       °
  260.  rompt.'''                                                                     °
  261.   '''Any text entered there is returned by FOO together with a string of       °
  262.  '''                                                                           °
  263.   '''blanks of the same length as the prompt. In other words, the assign       °
  264.  ment'''                                                                       °
  265.   '''to the quote quad permits entering of character data from the keybo       °
  266.  ard'''                                                                        °
  267.   '''on the same line as the prompt. To work properly, the two expressio       °
  268.  ns''' '''containing æ must follow immediately each other.''' 'do'             °
  269.   '''     PROMPT REPLACEMENT'',r'                                              °
  270.   '''Normally, the leading prompt blanks are not desired in the result.'       °
  271.  '' '''Here is one way to remove them'',r'                                     °
  272.   'T╜ÉFX ''U╜FOO W'' ''æ╜W'' ''U╜(µW)╟æ''' '1 showfn ''FOO'''                  °
  273.   'show ''CH╜FOO ''''Enter anything'''''' ''DISPLAY CH'''                      °
  274.   '''There is a System variable called PROMPT REPLACEMENT that can be us       °
  275.  ed'''                                                                         °
  276.   '''to specify how to deal with the prompt. This variable is stored in        °
  277.  ÉPR,'''                                                                       °
  278.   '''and contains a blank as a default. ÉPR can be assigned any SINGLE''       °
  279.  '                                                                             °
  280.   '''special character, or a null string. Here is an example of function       °
  281.  '''                                                                           °
  282.   '''FOO taking as a left argument the character to be used as a prompt'       °
  283.  '' '''replacement'''                                                          °
  284.   'T╜ÉFX ''U╜V FOO W;ÉPR'' ''ÉPR╜V'' ''æ╜W'' ''U╜æ'''                          °
  285.   '1 showfn ''FOO'''                                                           °
  286.   'show ''CH╜''''*'''' FOO ''''Enter anything'''''' ''DISPLAY CH'''            °
  287.   'show ''CH╜'''''''' FOO ''''Enter anything'''''' ''DISPLAY CH'''             °
  288. X '''A null string will retain the prompt string''' 'endd'                     °
  289. *(1997 6 23 12 34 47 556)                                                      °
  290.  FTEACH2 ÉFX 'TEACH2;NAME;FN;N;T' 'Σ Quad utility'                             °
  291.   '''As you begin to develop APL2 applications, you will find the need t       °
  292.  o'''                                                                          °
  293.   '''use both the quad and the quote quad. Typically, just before asking       °
  294.  '''                                                                           °
  295.   '''for user input, there has to be some sort of a text statement defin       °
  296.  ing''' '''the nature of data to be entered (ENTER AS REQUESTED)'',r'          °
  297.   '''      Enter two numbers in the range 1 to 100'''                          °
  298.   'show ''NO╜É'' ''2⌡NO''' '''      What is your name?'''                      °
  299.   'show ''NAME╜æ'' ''ΦNAME'''                                                  °
  300.   '''The need for a prompting phrase is so common that it makes sense to       °
  301.  '''                                                                           °
  302.   '''have a special APL2 defined utility for that purpose. Let us call''       °
  303.  ' '''a utility function to prompt for numeric values GETNO'',r'               °
  304.   'show ''ÉFX ''''U╜GETNO W'''' ''''Σ Prompt for numeric data'''' ''''W        °
  305.  ╪ U╜É''''''' '1 showfn ''GETNO'''                                             °
  306.   '''Function GETNO prints the prompt first, and then applies the quad''       °
  307.  ,r'                                                                           °
  308.   'show ''N╜GETNO ''''Enter your age in years'''''' ''T╜''''You are'''',       °
  309.  N,''''years old''''''' 'show ''DISPLAY T'''                                   °
  310.   '''Since we desire to make function GETNO a useful utility, let us con       °
  311.  sider'''                                                                      °
  312.   '''what additional features it should have. Let us agree that GETNO sh       °
  313.  ould'''                                                                       °
  314.   '''prompt for a numeric scalar or vector entry. Beyond that we may wis       °
  315.  h to''' '''specify the following:'',r'                                        °
  316.   '''  a. If a vector, the number of elements in the vector'''                 °
  317.   '''  b. Whether the values are to be integers. If fractions, then spec       °
  318.  ify''' '''     the number of decimal digits.'''                               °
  319.   '''  c. The largest allowed values'''                                        °
  320.   '''  d. The smallest allowed values'''                                       °
  321.   '''  e. What to do if some entries are outside permitted range'',r'          °
  322.   '''To support options (a to d) we should add a left argument to GETNO'       °
  323.  ',r'                                                                          °
  324.   'FN╜''U╜V GETNO W;E'' ''Σ Prompt for numeric data'' ''W ╪ U╜É ╪ ╕(0=ÉN       °
  325.  C ''''V'''')/0''' 'show ''πFN'''                                              °
  326.   '''If the left argument is absent (i.e. GETNO is used monadically) the       °
  327.  n''' '''no further processing will be done'',r'                               °
  328.   'show ''FN╜FN,Γ''''L0:╕((╞V)≤µ,U)/L1 ╪ U╜U,É ╪ ╕L0'''''''                    °
  329.   '''If the number of entries is less than the specified value in the fi       °
  330.  rst'''                                                                        °
  331.   '''element of the left argument, then request additional values'''           °
  332.   'do'                                                                         °
  333.   'show ''FN╜FN,Γ''''L1:U╜(╞V)╞U ╪ ╕(1=µ,V)/0 ╪ U╜(╛.5+U⌡10*V[2])÷10*V[2       °
  334.  ]'''''''                                                                      °
  335.   '''Save only as many values as specified in the left argument. Then, i       °
  336.  f the'''                                                                      °
  337.   '''left argument has only one element, exit. Else do a half-adjust in        °
  338.  the''' '''last decimal position.''' 'do'                                      °
  339.   'show ''FN╜FN,Γ''''╕(2=µV)/0 ╪ V╜(4⌐µV)╞V'''''''                             °
  340.   '''If the left argument has only two elements, exit. Else make sure th       °
  341.  at'''                                                                         °
  342.   '''the left argument has at least four elements (This provides a defau       °
  343.  lt''' '''of zero for the smallest permissible value.)'',r'                    °
  344.   'show ''FN╜FN,Γ''''E╜(U>V[3])-U<V[4] ╪ ╕(^/E=0)/0'''''''                     °
  345.   '''If the values of the vector are all within range, exit. Else contin       °
  346.  ue'',r'                                                                       °
  347.   'show ''FN╜FN,Γ''''V╜6╞V,V[3 4] ╪ U[(E=1)/∞µE]╜V[5] ╪ U[(E=²1)/∞µE]╜V[       °
  348.  6]'''''''                                                                     °
  349.   '''If the left argument has only four elements, use the values of V[3        °
  350.  4]'''                                                                         °
  351.   '''as replacement defaults. Replace values outside range by upper and'       °
  352.  '' '''lower bounds.'',r' 'show ''ÉFX FN''' '1 showfn ''GETNO'''               °
  353.   'show ''5 GETNO ''''Enter numbers two at a time'''''''                       °
  354.   '''The function requested new values until you entered 6. Then it save       °
  355.  d only 5'',r' 'show ''2 4 GETNO ''''Enter: *2 3 4'''''''                      °
  356.   '''The function returned results to 4 decimal points'',r'                    °
  357.   'show ''10 0 20 10 ²2 ²1 GETNO ''''Enter: 3⌡∞10'''''''                       °
  358.   '''If you entered the requested expression, function GETNO returns a''       °
  359.  '                                                                             °
  360.   '''vector of length 10 with negative values in positions where the'''        °
  361.   '''expression generates values outside the desired range (10 to 20).''       °
  362. X' 'endd'                                                                      °
  363. *(1997 6 23 12 50 19 508)                                                      °
  364.  FTEACH3 ÉFX 'TEACH3;N;C;FN' 'Σ Format, Execute and the DOIF utility'          °
  365.   '''      THE EXECUTE AND FORMAT FUNCTIONS'',r'                               °
  366.   '''The EXECUTE primitive (») provides the means of converting valid AP       °
  367.  L2''' '''character string expressions into evaluated results.'',r'            °
  368.   'show ''C╜''''1.56 2 34'''''' ''µC'' ''DISPLAY C'' ''N╜»C'' ''µN'' ''D       °
  369.  ISPLAY 2⌡N'''                                                                 °
  370.   '''In the example we converted the character string C representing a''       °
  371.  '                                                                             °
  372.   '''numeric vector to the numeric vector N. In commercial applications'       °
  373.  ''                                                                            °
  374.   '''this is very useful, since numeric data is commonly stored in files       °
  375.  '''                                                                           °
  376.   '''as character strings. To store numeric arrays in files we also need       °
  377.  '''                                                                           °
  378.   '''the converse function. In APL2 this process is done with the FORMAT       °
  379.  '''                                                                           °
  380.   '''primitive function. The monadic version of format is given by the s       °
  381.  ymbol'''                                                                      °
  382.   '''«, also called the thorn. (We will study the dyadic version of form       °
  383.  at''' '''in a later session.)'',r' 'show ''µÉ╜«N'' ''DISPLAY «N'''            °
  384.   '''These two functions are dual for vectors only if we begin with the'       °
  385.  '' '''format of a numeric vector. Here is why'',r'                            °
  386.   'show ''µC╜''''1     2     3'''''' ''µ«»C'''                                 °
  387.   '''The starting character string need not comply with the strict rules       °
  388.  '''                                                                           °
  389.   '''for conversion of a numeric vector to a string using the « function       °
  390.  .''' 'do'                                                                     °
  391.   '''For that matter, the character expression need not even be a symbol       °
  392.  ''' '''representation of a numeric vector. It can be ANY valid APL2'''        °
  393.   '''expression'',r'                                                           °
  394.   'show ''C╜''''+/5 4µ6+∞15'''''' ''N╜»C'' ''DISPLAY N'''                      °
  395.   '''Some APL2 theoreticians are uneasy with the execute command, becaus       °
  396.  e'''                                                                          °
  397.   '''it tends to be misused. In some studies of user defined functions i       °
  398.  t'''                                                                          °
  399.   '''was found that the execute can frequently be avoided by a more care       °
  400.  fully''' '''thought out algorithm.'',r'                                       °
  401.   '''There are times, however, when the execute can be a very powerful t       °
  402.  ool.'''                                                                       °
  403.   '''Here is an example of a utility function that uses the execute. (No       °
  404.  te'''                                                                         °
  405.   '''that in this workspace, utility functions have been defined in lowe       °
  406.  r''' '''case.)'',r' '1 showfn ''doif'''                                       °
  407.   '''NOTE: Actually this function has become obsolescent with the'''           °
  408.   '''      introduction of the DIAMOND (╪) separator. (We will study'''        °
  409.   '''      the System function ÉEA in later sessions, but for the time''       °
  410.  '                                                                             °
  411.   '''      being, the left argument of ÉEA is evaluated only if the righ       °
  412.  t''' '''      argument fails.)''' 'do'                                        °
  413.   '''Function ''''doif'''' is dyadic. It tests the left argument which m       °
  414.  ust'''                                                                        °
  415.   '''return a boolean scalar or vector. If the result of the test is 1''       °
  416.  '                                                                             °
  417.   '''then the right argument is executed. The function returns the boole       °
  418.  an''' '''result'',r' 'show ''N╜2'' ''C╜(N<10) doif ''''∞N'''''' ''C'''        °
  419.   '''The right argument must be valid APL2 expression as a character str       °
  420.  ing.''' '''It may even be a vector of APL2 expressions'',r'                   °
  421.   'show ''N╜2 12 3'' ''C╜(N<10) doif ''''∞2'''' ''''∞3'''' ''''∞4''''''        °
  422. X''C''' 'endd'                                                                 °
  423. *(1997 6 24 11 41 32 524)                                                      °
  424.  FTEACH4 ÉFX 'TEACH4;RL' 'Σ Random link and Time stamp' 'RL╜?100'              °
  425.   '''    RANDOM LINK AND TIME STAMP'',r'                                       °
  426.   '''We have previously studied the functions ROLL and DEAL, but we have       °
  427.  '''                                                                           °
  428.   '''not discussed how these functions generate random values. Ignoring        °
  429.  the'''                                                                        °
  430.   '''actual algorithm, there is a SYSTEM variable called RANDOM LINK tha       °
  431.  t''' '''determines the actual sequence of random numbers.'',r'                °
  432.   '''Random link is stored in ÉRL. It will accept integer values and wil       °
  433.  l'''                                                                          °
  434.   '''return its current value which changes each time the roll or deal''       °
  435.  ' '''function is invoked'',r'                                                 °
  436.   'show ''RL╜É╜ÉRL'' ''?5µ100'' ''ÉRL'' ''ÉRL╜RL'' ''?3µ100'' ''?2µ100''       °
  437.   ''ÉRL'''                                                                     °
  438.   '''One consequence of this is that the  "random results" in a workspac       °
  439.  e'''                                                                          °
  440.   '''may ALWAYS return the same sequence, each time that workspace is lo       °
  441.  aded.'''                                                                      °
  442.   '''This is because the value of ÉRL is stored with the workspace.'''         °
  443.   'do'                                                                         °
  444.   '''In some applications this may not be desired. How can the random li       °
  445.  nk'''                                                                         °
  446.   '''be INITIALIZED when the workspace is loaded? One way of doing it is       °
  447.   to'''                                                                        °
  448.   '''use another SYSTEM function called the TIME STAMP. The time stamp''       °
  449.  '                                                                             °
  450.   '''(ÉTS) generates a numeric vector of length 7. The values represent        °
  451.  the'''                                                                        °
  452.   '''YEAR, MONTH, DAY, HOUR(24), MINUTE, SECOND, and MILISECOND of the i       °
  453.  nstant''' '''the time stamp was invoked'',r' 'show ''ÉTS'' ''ÉTS'''           °
  454.   '''Since the time when a workspace is loaded is unpredictable, it is a       °
  455.  '''                                                                           °
  456.   '''convenient way of extracting a random link. To do that, simply ente       °
  457.  r'''                                                                          °
  458.   '''an expression such as the following in the appropriate function'',r       °
  459. X' 'show ''RL╜ÉRL╜+/²3╞ÉTS''' 'endd'                                           °
  460. *(1997 6 24 12 41 26 504)                                                      °
  461.  FTEACH5 ÉFX 'TEACH5;DECK;T;FN' 'Σ The game of BLACKJACK'                      °
  462.   '''We will now illustrate the usage of random link and the time stamp'       °
  463.  '' '''by developing an application of the game of blackjack.''' 'do'          °
  464.   '''       SHUFFLING, DISPLAYING AND DEALING CARDS'',r'                       °
  465.   '''As in any card game, cards have to be shuffled and then dealt to th       °
  466.  e'''                                                                          °
  467.   '''players. We must, therefore, begin by defining a card deck, definin       °
  468.  g'''                                                                          °
  469.   '''the process of shuffling cards, and the process of dealing cards.''       °
  470.  ,r'                                                                           °
  471.   '''NOTE: You may wish to use the font... option in the options pull do       °
  472.  wn'''                                                                         °
  473.   '''      menu to increase the size of your font (12 or 14 should do it       °
  474.  ).''' 'do'                                                                    °
  475.   '''Since a card deck contains 52 cards, it makes sense to associate'''       °
  476.   '''each card with an integer in the interval (1,52). We will use the''       °
  477.  '                                                                             °
  478.   '''following scheme: For integers 1-13 we will associate CLUBS, for'''       °
  479.   '''14-26 DIAMONDS, for 27-39 HEARTS, and for 40-52 SPADES. Within each       °
  480.  '''                                                                           °
  481.   '''suit, the sequence will be 2,3,4,5,6,7,8,9,10,J,Q,K,A. In this way'       °
  482.  ''                                                                            °
  483.   '''the integer 10, for example will represent the JACK of CLUBS, and''       °
  484.  ' '''the integer 28 will represent the 3 of HEARTS.''' 'do'                   °
  485.   '''We define a variable called DECK to contain all cards in a deck'',r       °
  486.  ' 'show ''DECK╜∞52''' '''We next shuffle this deck'',r'                       °
  487.   'show ''ÉRL╜+/²3╞ÉTS'' ''DECK╜DECK[52?52]'' ''4 13µDECK'''                   °
  488.   '''We have shuffled the deck of cards, but it is difficult to see what       °
  489.  '''                                                                           °
  490.   '''these integers stand for. What we need is a display function'',r'         °
  491.   'FN╜''U╜SHOW C'' ''Σ Display cards C'''                                      °
  492.   'FN╜FN,Γ''U╜(3Φ''''JQKA23456789'''',Γ''''10'''')[1+13|C]'''                  °
  493.   'FN╜FN,Γ''U╜U,■(ÉAV[6 5 4 7])[⌐C÷13]'' ╪ πFN ╪ T╜ÉFX FN' 'do'                °
  494.   '''We can now display cards from this deck'',r'                              °
  495.   'show ''2╞DECK'' ''SHOW 2╞DECK'''                                            °
  496.   '''Next we need a function to deal cards from this deck'',r'                 °
  497.   'FN╜''U╜DEAL W'' ''Σ Deal W cards'' ''U╜W╞DECK'' ''DECK╜W╟DECK'' ╪ πFN       °
  498.   ╪ T╜ÉFX FN' 'r' 'show ''SHOW 4 5µDEAL 20'' ''µDECK'''                        °
  499.   'r,''(Function DEAL does not handle the case when the deck contains fe       °
  500.  wer'''                                                                        °
  501.   '''cards than requested. This would be needed in the game of poker.)''       °
  502.  ,r' 'do' '''        THE GAME OF BLACKJACK'',r'                                °
  503.   '''Let us now look at the basic rules of BLACKJACK. For simplicity, we       °
  504.  '''                                                                           °
  505.   '''will assume there are only two players: Dealer and Player. We will'       °
  506.  '' '''also ignore betting in this sample game.'',r'                           °
  507.   ''' A. Dealer shuffles the deck, and deals two cards each. (We will sh       °
  508.  ow'''                                                                         °
  509.   '''    all cards as dealt, and deal to Player first which is not the w       °
  510.  ay''' '''    the actual game is played.)'',r'                                 °
  511.   ''' B. The value of cards is that of their pip (number on the card)'''       °
  512.   '''    Face cards count 10, and the Ace can be counted as 1 OR 11'',r'       °
  513.   '''   DECISION RULES'',r'                                                    °
  514.   '''    1. If player sees a 10 value card and an ACE, player says'''          °
  515.   '''       BLACKJACK and wins.'''                                             °
  516.   '''    2. If cards ad up to 15 or more, but less than 22, player says'       °
  517.  ''                                                                            °
  518.   '''       I hold. Action then passes to the next player (Dealer).'''         °
  519.   '''    3. If cards in players hand add up to 22 or more player LOSES.'       °
  520.  ''                                                                            °
  521.   '''    4. Else player says HIT ME, gets another card, and goes to rule       °
  522.   2'',r'                                                                       °
  523.   ''' C. After Player stops, Dealer''''s cards are evaluated under the''       °
  524.  ' '''    same rules. EXCEPTION: in case of draw, Dealer wins.''' 'do'         °
  525.   '''       TWO MORE UTILITIES'',r'                                            °
  526.   '''Before we define function BLACKJACK, we need two more defined objec       °
  527.  ts.'''                                                                        °
  528.   '''We need a function to evaluate a blackjack hand, and we need an'''        °
  529.   '''operator that can be used to determine what to do with a given hand       °
  530.  .'',r' '''Let us call the function to evaluate a hand EVAL'',r'               °
  531.   'FN╜''U╜ EVAL W;R'' ''Σ Evaluate a blackjack hand'''                         °
  532.   'FN╜FN,Γ''U╜+/((∞10),3µ10)[1+R╜13|W]'''                                      °
  533.   'FN╜FN,Γ''╕(~0εR)/0 ╪ U╜0¥U,(22>U+10)/U+10'' ╪ πFN ╪ T╜ÉFX FN'               °
  534.   'r,''The last expression in EVAL takes care of a hand containing aces'       °
  535.  ',r' 'show ''SHOW 13 26 39'' ''EVAL 13 26 39'''                               °
  536.   '''Finally, we define the operator ISIN. This operator returns a boole       °
  537.  an''' '''scalar in response to a range inquiry'',r'                           °
  538.   'FN╜''U╜V (f ISIN) W'' ''Σ Do cards W add up to range V'''                   °
  539.   'FN╜FN,''U╜(0¥ΦV) f W'' ''╕(1=µ,V)/0'' ''U╜U^W f V[2]'' ╪ πFN ╪ T╜ÉFX        °
  540.  FN'                                                                           °
  541.   'r,''Notice that in the last line the W preceeds V[2] in the compariso       °
  542.  n.''' '''Let us see how ISIN works'',r'                                       °
  543.   'show ''21 =ISIN 21'' ''14 21 <ISIN É╜13+∞9'' ''15 >ISIN É╜12+∞4'''          °
  544. X 'endd'                                                                       °
  545. *(1997 6 27 13 43 42 592)                                                      °
  546.  FTEACH6 ÉFX 'TEACH6;T;FN'                                                     °
  547.   'Σ Using utility ''doif'' in function BLACKJACK'                             °
  548.   '''       A PROGRAMMING STYLE EDITORIAL'',r'                                 °
  549.   '''In what follows, we will illustrate a programming style. Since this       °
  550.  '''                                                                           °
  551.   '''subject does not describe new APL2 primitives or its architecture,'       °
  552.  '' '''this section is labelled as an editorial.''' 'do'                       °
  553.   '''The object of this exercise is to display one possible programming'       °
  554.  ''                                                                            °
  555.   '''style that one might use in solving a particular problem. We could'       °
  556.  ''                                                                            °
  557.   '''define function BLACKJACK "conventionally" by writing code that'''        °
  558.   '''makes no distinction between the game, the logic and the rules of''       °
  559.  '                                                                             °
  560.   '''the game. Indeed as an exercise, you should try to define such a'''       °
  561.   '''function at the end of this lesson. (Such conventional version of''       °
  562.  ' '''blackjack, written by Harry Bertucelli is available.)''' 'do'            °
  563.   '''      A SAMPLE EXPERT SYSTEM'',r'                                         °
  564.   '''An early version of an Expert System, was first introduced by'''          °
  565.   '''Post in 1943. It is called a PRODUCTION SYSTEM. We will design the'       °
  566.  ''                                                                            °
  567.   '''card game of BLACKJACK using this Production System approach.'',r'        °
  568.   'do'                                                                         °
  569.   '''We begin by defining function INFEN (for INFerence ENgine). This'''       °
  570.   '''function will will combine the rules of the game AND its logic'',r'       °
  571.   'show ''FN╜''''U╜INFEN W;P;T'''' ''''Σ Blackjack rules and logic'''' '       °
  572.  '''U╜0'''''''                                                                 °
  573.   '''Initialization includes setting U to zero --to indicate continuatio       °
  574.  n'''                                                                          °
  575.   '''of the game. The right argument to INFEN is a nested string of word       °
  576.  s'''                                                                          °
  577.   '''that apply to a specific player. For the player, these would be:'''       °
  578.   '''(Player, I win, I fold, Hit me, I stand, BLACKJACK).''' 'do'              °
  579.   'show ''FN╜FN,Γ'''' W[1],''''''''gets'''''''' 2 ''''''''cards:''''''''       °
  580.   ╪ SHOW P╜DEAL 2'''''''                                                       °
  581.   '''The game is initialized. Two cards are dealt and displayed.'''            °
  582.   'do'                                                                         °
  583.   'show ''FN╜FN,Γ''''L0:╕(21⌠ISIN R╜EVAL P)/L1 ╪ W[6 2] ╪ U╜1 ╪ ╕0''''''       °
  584.  '                                                                             °
  585.   '''If the cards add up to 21, the game is over. The value of U is set        °
  586.  to'''                                                                         °
  587.   '''one to indicate that fact. Else if R<21, more tests have to be made       °
  588.  .'''                                                                          °
  589.   '''Variable R which contains the value of the hand is GLOBAL to functi       °
  590.  on''' '''INFEN. (This is called a "side effect".)''' 'do'                     °
  591.   'show ''FN╜FN,Γ''''L1:╕(21>ISIN R)/L2 ╪ U╜1 ╪ W[3] ╪ ╕0'''''''               °
  592.   '''If R is greater than 21, player loses. The value of U is also set t       °
  593.  o''' '''one to indicate the end of the game.''' 'do'                          °
  594.   'show ''FN╜FN,Γ''''L2:╕(~14 21<ISIN R)/L3 ╪ ΓW[5] ╪ ╕0'''''''                °
  595.   '''If the value is greater than 14 but less than 21, Player says "I st       °
  596.  and"''' 'do'                                                                  °
  597.   'show ''FN╜FN,Γ''''L3:W[4] ╪ T╜æ ╪ SHOW P╜P,DEAL 1 ╪ ╕L0'''''''              °
  598.   '''If the value is less than 15, player says "hit me", and pauses. The       °
  599.  n a'''                                                                        °
  600.   '''new card is shown to the player and evaluation is repeated.'''            °
  601.   'T╜ÉFX FN' 'do' '''Here is a listing of this function'''                     °
  602.   '1 showfn ''INFEN''' 'do'                                                    °
  603.   '''Before we can try out this function, we must define DECK'',r'             °
  604.   'show ''DECK╜52?52'''                                                        °
  605.   'show ''INFEN ''''Player'''' ''''I win'''' ''''fold'''' ''''Hit me''''       °
  606.   ''''I stand'''' ''''BJ'''''''                                                °
  607.   '''Function INFEN works. Now we can define the niladic function BLACKJ       °
  608.  ACK.'''                                                                       °
  609.   '''In my teminology a niladic function is called a COMMAND. It serves        °
  610.  as''' '''an entry to an application.'',r'                                     °
  611.   'FN╜''BLACKJACK ;D;DECK;T;S;R'' ''Σ Command for game of Blackjack'''         °
  612.   'FN╜FN,Γ''DECK╜∞52 ╪ DECK╜DECK[52?52]'''                                     °
  613.   'FN╜FN,Γ''D╜''''Player'''' ''''I win'''' ''''I fold'''' ''''Hit me''''       °
  614.   ''''I stand'''' ''''BLACKJACK'''''''                                         °
  615.   'FN╜FN,Γ''╕(INFEN D)/0 ╪ T╜æ ╪ ╕(21<S╜R)/0'''                                °
  616.   'FN╜FN,Γ''D╜(5╞''''Dealer'''' ''''You lose'''' ''''You win''''),5╟D ╪        °
  617.  ╕(INFEN D)/0''' 'FN╜FN,''╕(S≥R)/L0 ╪ D[2] ╪ ╕0'' ''L0:D[3]'' ╪ ÉFX FN'        °
  618.   '1 showfn ''BLACKJACK'''                                                     °
  619.   '''Function BLACKJACK first defines a card deck and shuffles it. Then        °
  620.  a'''                                                                          °
  621.   '''dictionary of terms used by the player is stored in local variable        °
  622.  D.'''                                                                         °
  623.   '''If Player does not end the game (by getting a BLACKJACK or by losin       °
  624.  g),'''                                                                        °
  625.   '''then the game continues. The player''''s score is saved in S (recal       °
  626.  l that'''                                                                     °
  627.   '''R is a "side effect" of function INFEN). A pause is produced by ass       °
  628.  igning'''                                                                     °
  629.   '''a quote quad to T. The dictionary is updated to that of the dealer,       °
  630.   and''' '''function INFEN is invoked for the dealer.'',r' 'do'                °
  631.   '''Here is a sample run of function BLACKJACK'',r'                           °
  632. X 'show ''BLACKJACK''' 'endd'                                                  °
  633. *(1997 6 30 20 48 20 564)                                                      °
  634.  FTEACH7 ÉFX 'TEACH7;F;FN;T' 'Σ Another version of BLACKJACK'                  °
  635.   '''     A RULE BASED APPROACH'',r'                                           °
  636.   '''Function BLACKJACK works fine, but the decision making is hardwired       °
  637.  '''                                                                           °
  638.   '''in function INFEN. There is yet another way to solve this game. The       °
  639.  '''                                                                           °
  640.   '''idea is to write a UTILITY function called INFEN2, and to store the       °
  641.  ''' '''rules of the game in nested arrays.''' 'do'                            °
  642.   'FN╜''INFEN2 W;N;ANTE ;CONS'' ''Σ A simple Inference Engine'' ''(ANTE        °
  643.  CONS)╜W ╪ N╜0'''                                                              °
  644.   'T╜ÉFX FN,Γ''L0:╕(²1=N)/0 ╪ N╜N+1 ╪ ╕(~U╜0¥»NπANTE)/L0 ╪ »NπCONS ╪ ╕L0       °
  645.  ''' '''We now illustrate this approach''' '1 showfn ''INFEN2'''               °
  646.   '''The right argument W is a nested array. Its two components are the'       °
  647.  ''                                                                            °
  648.   '''antecedents and the consequents of a set of rules. The two local'''       °
  649.   '''variables ANTE and CONS are initialized, as well as the rule counte       °
  650.  r N.'',r'                                                                     °
  651.   '''The first rule to be looked at is rule 1. The function evaluates th       °
  652.  e'''                                                                          °
  653.   '''first element of the nested string ANTE (for Antecedent). If this r       °
  654.  eturns'''                                                                     °
  655.   '''a 1 (TRUE), then the first element of nested string CONS (for Conse       °
  656.  quent)'''                                                                     °
  657.   '''will be evaluated next. Note that if NπCONS contains an expression        °
  658.  of''' '''the form''' '''       N╜²1   or   (...,N)╜...,²1'''                  °
  659.   '''then on branchimg back to L0, there will be an exit from INFEN2. Th       °
  660.  e'''                                                                          °
  661.   '''number of rules evaluated by INFEN2, will depend entirely on the'''       °
  662.   '''contents of arrays ANTE and CONS. The elements of ANTE must return        °
  663.  a'''                                                                          °
  664.   '''boolean value, but the elements of CONS may produce arbitrary side'       °
  665.  '' '''effects.'',r'                                                           °
  666.   '''Note that function INFEN2 is perfectly general. It could be used fo       °
  667.  r'''                                                                          °
  668.   '''any other "rule based" application. Indeed, The consequents of a ru       °
  669.  le'''                                                                         °
  670.   '''set may invoke INFEN2 recursively using additional rule sets.'''          °
  671.   'do' '''Let us now see how we must re-design function BLACKJACK.'''          °
  672.   'FN╜''BLACKJACK2 ;D;DECK;T;S;R'' ''Σ Command for game of Blackjack'''        °
  673.   'FN╜FN,Γ''DECK╜∞52 ╪ DECK╜DECK[52?52] ╪ P╜DEAL 2'''                          °
  674.   'FN╜FN,Γ''D╜''''Player'''' ''''I win'''' ''''I fold'''' ''''Hit me''''       °
  675.   ''''I stand'''' ''''BLACKJACK'''''''                                         °
  676.   'FN╜FN,Γ''INFEN2 RULES ╪ T╜æ ╪ ╕(21<S╜R)/0'''                                °
  677.   'FN╜FN,Γ''D╜(5╞''''Dealer'''' ''''You lose'''' ''''You win''''),5╟D ╪        °
  678.  INFEN2 RULES'''                                                               °
  679.   'FN╜FN,''╕(S≥R)/L0 ╪ D[2] ╪ ╕0'' ''L0:D[3]'' ╪ T╜ÉFX FN'                     °
  680.   '1 showfn ''BLACKJACK2'''                                                    °
  681.   '''This function is essentially the same as BLACKJACK. The only differ       °
  682.  ence'''                                                                       °
  683.   '''is in function INFEN2. Its right argument now is the array of rules       °
  684.  .''' 'do' '''Here are the simple rules of blackjack'',r'                      °
  685.   '''RULE  ANTECEDENT                CONSEQUENT'',r'                           °
  686.   ''' 1       1          D[1] ╪ ''''gets'''' 2 ''''cards:'''' ╪ SHOW P╜D       °
  687.  EAL 2 ╪ T╜æ''' ''' 2 21=ISIN R╜EVAL P D[6 2] ╪ N╜²1'''                        °
  688.   ''' 3 21<ISIN R╜EVAL P D[3] ╪ N╜²1'''                                        °
  689.   ''' 4 14 22<ISIN R     D[5] ╪ N╜²1'''                                        °
  690.   ''' 5       1          D[4] ╪ SHOW P╜P,DEAL 1 ╪ N╜1 ╪ T╜æ'',r' 'do'          °
  691.   'ANTE╜''1'' ''21=ISIN R╜EVAL P'' ''21<ISIN R╜EVAL P'' ''14 22<ISIN R''       °
  692.   ''1'''                                                                       °
  693.   'CONS╜Γ''D[1] ''''gets'''' 2 ''''cards:'''' ╪ SHOW P╜DEAL 2 ╪ T╜æ'''         °
  694.   'CONS╜CONS,''D[6 2] ╪ N╜²1'' ''D[3] ╪ N╜²1'' ''D[5] ╪ N╜²1'''                °
  695.   'CONS╜CONS,Γ''D[4] ╪ SHOW P╜P,DEAL 1 ╪ T╜æ ╪ N╜2''' 'RULES╜ANTE CONS'        °
  696. X 'show ''DISPLAY π1πRULES'' ''DISPLAY π2πRULES'' ''BLACKJACK''' 'endd'        °
  697. *(1997 7 13 12 28 49 504)                                                      °
  698.  Faddquote ÉFX 'u╜addquote w'                                                  °
  699.   'Σ Put quotes around a string, and double existing quotes'                   °
  700. X 'u╜ÉAV[40],((1+w=ÉAV[40])/w),ÉAV[40]'                                        °
  701. *(1997 7 24 13 20 38 476)                                                      °
  702.  Fav ÉFX 'av;A;N;I;ÉIO' 'Σ Display characters in ÉAV' 'ÉIO╜0'                  °
  703.   'A╜22 78µ'' ''' 'N╜3 0«φ12 22µ1+∞356' 'A[;,(6⌡∞12)°.+2 3 4]╜N'               °
  704.   'A[;6+6⌡∞12]╜φ12 22µÉAV' 'ΣA[8 10 13;6]╜'' ''' 'A[13;6]╜'' '''               °
  705. X 'A[14+∞8;68 69 70 72]╜'' ''' 'A'                                             °
  706. *(1991 11 11 8 25 13 316)                                                      °
  707.  Fdate ÉFX 'u╜date w' 'Σ Format date and time of day' 'u╜«6╞w'                 °
  708. X 'u╜('' ''⌠u)Γu' 'u╜εu,■''-- .. '''                                           °
  709. XCdig 1 10 1234567890                                                          °
  710. *(1997 9 9 13 0 45 372)                                                        °
  711.  Fdisclaimer ÉFX 'disclaimer' 'Σ Copyright statement'                          °
  712.   '(10µ'' ''),''Copyright, Z. V. Jizba, 1995,1996,1997'',r'                    °
  713.   ''' This and subsequent workspaces labelled TEACHxx are made available       °
  714.  '''                                                                           °
  715.   '''at no cost to anyone who desires to learn how to use effectively'''       °
  716.   '''the IBM/OS2 version of APL2.'',r'                                         °
  717.   '''This software is provided "AS IS" with no WARRANTY of any kind, eit       °
  718.  her'''                                                                        °
  719.   '''express or implied. Any risk in its use resides with you, the user        °
  720.  of''' '''these tutorials.'',r' '''     ACKNOWLEDGEMENTS'',r'                  °
  721.   ''' In writing these tutorials, I am greatly indebted to Roy Sykes, wh       °
  722.  ose'''                                                                        °
  723.   '''excellent lectures increased my understanding of the language.'''         °
  724.   '''Discussions with the late Harry Bertucelli clarified a number of'''       °
  725.   '''concepts and caused me to change some improper terminology that was       °
  726.  '''                                                                           °
  727.   '''used in previous versions of these tutorials. Mr. Benjamin Archer''       °
  728.  '                                                                             °
  729.   '''kindly checked out a nearly final version, bringing to my attention       °
  730.  ''' '''some ommisions, misspellings, and invalid terminology.'',r'            °
  731. X '''(PRESS ENTER to continue)'''                                              °
  732. *(1997 7 13 12 28 50 508)                                                      °
  733.  Fdo ÉFX 'do;T;E' 'Σ Expression driver' 'E╜''''' 'æ╜''      ''' 'T╜æ'          °
  734.   '╕(^/'' ''=T)/0'                                                             °
  735.   '╕(('':''εT)doif ''B╜evaldd (+/^\'''' ''''=T)╟T'')/2'                        °
  736.   '''E╜ÉEM'' ÉEA T' '╕(0=µ,E)/2'                                               °
  737.   '''This is not a valid APL2 expression''' 'æ╜''*''' '╕(''?''⌠╞1╟æ)/2'        °
  738. X 'E' '╕2'                                                                     °
  739. *(1997 7 13 12 28 50 508)                                                      °
  740.  Fdoif ÉFX 'U╢╜V╢ doif W╢;t╢' 'Σ Rule' '╕(^/~U╢╜V╢)/0'                         °
  741. X '''U╢╜V╢ doif■ W╢'' ÉEA ''»V╢/W╢'''                                          °
  742. *(1997 9 9 12 50 14 444)                                                       °
  743.  Fendd ÉFX 'endd' 'Σ end of special feature' '20µ''²'' ╪ ╕(4<µÉLC)/0'          °
  744. X 'do'                                                                         °
  745. *(1997 8 17 11 35 51 556)                                                      °
  746.  Ferase ÉFX                                                                    °
  747.   'erase;t;EXIT;GO;HELP;DISPLAY;BIND;REVIEW;RULE;SHOW;DEAL;EVAL;ISIN;INF       °
  748.  EN;BLACKJACK;DISCLAIMER' 'Σ Erase all global functions and variables'         °
  749.   't╜ÉNL 3' 't╜(~t^.εlc,'' '')≡t' 't╜ÉEX(~t[;∞5]^.=''TEACH'')≡t'               °
  750. X 't╜ÉNL 2' 't╜ÉEX(~t^.εlc,'' '')≡t' 't╜ÉNL 4' 't╜ÉEX(~t^.εlc,'' '')≡t'        °
  751. *(1997 7 27 13 47 41 608)                                                      °
  752.  Fevaldd ÉFX 'u╜evaldd w;c;n' 'Σ Evaluate direct definition' 'u╜0'             °
  753.   'n╜(w∞''Σ'')-1' 'c╜(((n╞w)⌠'':'')Γn╞w),Γ''ΣDD '',(n+1)╟w'                    °
  754.   '╕((1 label╞c)doif ''''''Invalid label'''''')/0'                             °
  755.   '╕((2=µc)doif ''u╜showdd 1╙c'')/0'                                           °
  756.   '╕((3=ÉNC╞c)doif ''u╜⌡µÉ╜(╞c),'''' is already defined.'''''')/0'             °
  757.   '╕((3=µc)doif ''u╜simdd c'')/0' 'c╜(Γ''α∙ aw'')replace■c'                    °
  758.   'u╜ε''u╜'',((''a''εεc[2 3 4])/''a ''),(╞c),'' w;t;b'''                       °
  759.   'u╜u(5πc)(''b╜(t╜'',(3πc),'')/'',addquote ''u╜'',4πc)'                       °
  760. X 'u╜u,''╕(t doif b)/0''(''u╜'',2πc)' 'u╜╧ÉFX u'                               °
  761. *(1997 7 25 13 27 52 564)                                                      °
  762.  Fexit ÉFX 'V exit W;T' 'Σ Exit if too many suspended functions'               °
  763.   '╕(0⌠ÉNC ''V'')/L0 ╪ V╜10' 'L0:╕(V>µÉLC)/0'                                  °
  764.   '''There are too many suspended functions''' '''Please enter '',W'           °
  765. X '╕'                                                                          °
  766. *(1997 7 26 12 33 39 536)                                                      °
  767.  Fget ÉFX 'U╜V get W;t;T;ÉPR' 'Σ Prompt for response from keyboard'            °
  768.   'ÉPR╜T╜ÉAV[ÉIO+255] ╪ ╕(0⌠ÉNC ''V'')/L0 ╪ V╜1' 'L0:V╜V╧1' 'æ╜W ╪ t╜æ'        °
  769.   'U╜(+/^\t=T)╟t' '╕(''╕''⌠╞U)/L1 ╪ ╕' 'L1:╕V/0' 't╜(U⌠'' '')ΓU'               °
  770. X 'U╜(µt),(ΓU),t'                                                              °
  771. *(1997 7 28 13 33 8 424)                                                       °
  772.  Fglobals ÉFX 'globals' 'Σ Initialize useful global variables'                 °
  773.   'uc╜''ABCDEFGHIJKLMNOPQRSTUVWXYZ'''                                          °
  774.   'lc╜''abcdefghijklmnopqrstuvwxyz''' 'dig╜''1234567890'''                     °
  775. X 'r╜ÉAV[13+ÉIO]' 'q╜'''''''''                                                 °
  776. *(1997 7 3 12 47 6 368)                                                        °
  777.  Finitialize ÉFX 'initialize;T' 'Σ Initialize workspace'                       °
  778.   '''AT ALL TIMES, TO CONTINUE, PRESS RETURN!'',r'                             °
  779.   '''To see disclaimers enter:'',r,''    disclaimer''' 'do' 'erase'            °
  780.   'globals'                                                                    °
  781.   '''Make sure the CAP LOCK light on your keyboard (upper right) is ON!'       °
  782. X'' 'endd'                                                                     °
  783. *(1997 7 27 13 14 33 444)                                                      °
  784.  Flabel ÉFX 'u╜v label w'                                                      °
  785.   'Σ Return 1 if label w does not begin with a cap'                            °
  786.   '╕(0⌠ÉNC ''v'')/L0 ╪ v╜0' 'L0:v╜v╧1 ╪ w╜εw ╪ ╕v/L1 ╪ ╕(u╜0⌠ÉNC w)/0'         °
  787. X 'L1:╕(u╜~^/wεlc,uc,dig)/0' 'u╜w[1]εlc,dig'                                   °
  788. XClc 1 26 abcdefghijklmnopqrstuvwxyz                                           °
  789. *(1997 7 13 12 28 55 528)                                                      °
  790.  Fnon ÉFX 'non;T;RC;ET;R' 'Σ Ignore keyboard entry' 'æ╜''      ''' 'T╜æ'       °
  791.   '╕(0=µ(T⌠'' '')/T)/0' '(RC ET R)╜ÉEC T' '╕(0=RC)/2'                          °
  792. X '╕((1=RC)doif ''R'')/2' '╕2'                                                 °
  793. *(1997 7 13 12 28 55 528)                                                      °
  794.  Fnotb ÉFX 'u╜notb w' 'Σ Remove trailing blanks'                               °
  795.   '╕((1<╧w)doif ''u╜notb■ w'')/0' '╕((1<µµw)doif ''u╜πnotb Γ[2]w'')/0'         °
  796. X 'u╜(1-(,'' ''⌠Φw)∞1)╟w'                                                      °
  797. *(1997 7 27 12 55 6 496)                                                       °
  798.  Fproblems ÉFX 'problems' 'Σ Problems'                                         °
  799.   '''That is all for this lesson. Remember, if you want to practice,'''        °
  800.   '''and plan to use direct definitions, be sure to first enter GO.'''         °
  801.   '''Direct definitions will then be accepted. To exit GO, enter EXIT.''       °
  802.  ,r'                                                                           °
  803.   '''To erase a previously defined DIRECT DEFINITION FUNCTION, enter'',r       °
  804.  ' '''       )ERASE functionname'',r'                                          °
  805. X '''WARNING! do not use )ERASE on other labels.'',r'                          °
  806. XCq 0 '                                                                        °
  807. XCr 0                                                                         °
  808. *(1997 7 13 12 28 56 532)                                                      °
  809.  Freplace ÉFX 'u╜v replace u;i;r;s' 'Σ Replace elements in v in u'             °
  810.   'i╜Γ∞µu' 's╜2πv╜(v⌠'' '')Γv' 'i╜⌡r╜i⌡■Γ[1]u°.=╞v'                            °
  811. X 'u[(εi)/εr]╜s[(εi)/εi⌡■∞µs]'                                                 °
  812. *(1997 7 13 12 28 56 532)                                                      °
  813.  Fround ÉFX 'U╜V round W' 'Σ Half adjust to V th decimal'                      °
  814. X 'U╜(╛0.5+W⌡10*V)÷10*V'                                                       °
  815. *(1997 7 13 12 28 57 536)                                                      °
  816.  Fshow ÉFX '╢V show ╢W;╢T;╢B' 'Σ Display and execute ╢W'                       °
  817.   '╢T╜(0=ÉNC ''╢V'')doif ''╢V╜0'''                                             °
  818.   '╕((0=╧╢W)doif ''show ╢W,'''' '''''')/0'                                     °
  819.   '╕((1<╧╢W)doif ''╢V show ■╢W'')/0' '''      '',╢W'                           °
  820. X '╕((╢V^'':''ε╢W)doif ''╢T╜evaldd ╢W'')/L0' '''ÉEM'' ÉEA ╢W' 'L0:do'          °
  821. *(1997 7 13 12 28 57 536)                                                      °
  822.  Fshowdd ÉFX 'u╜showdd w;a;b;c;r'                                              °
  823.   'Σ Display a direct definition function'                                     °
  824.   '╕((1=╧w)doif ''u╜showdd Γw'')/u╜0'                                          °
  825.   '╕((3⌠ÉNC╞w)doif ''(ε╞w),'''' is not a function'''''')/0'                    °
  826.   'c╜Γ[2]ÉCR╞w' 'c╜notb(2╞c),(Γ''aw α∙'')replace■2╟c'                          °
  827.   '╕((~''ΣDD''╧3╞2πc)doif ''''''Not a direct definition function'''''')/       °
  828.  0' 'u╜1' 'b╜('' ''⌠╞c)Γ╞c' 'a╜''      ''' 'r╜2╟3πc'                           °
  829.   '╕((3=µc)doif ''a,(╞w),'''':'''',r,(3<µ2πc)/''''   Σ'''',3╟2πc'')/0'         °
  830.   'a╜a,(╞w),'':'',(2╟5πc),'':''' 'b╜(+\r=''('')-+\r='')''' 'b╜b∞0'             °
  831. X 'a╜a,(²3╟(b-1)╞3╟r),'':'',2╟»(b+2)╟r' 'a,(3<µ2πc)/''  Σ'',3╟2πc'             °
  832. *(1997 7 13 12 28 57 536)                                                      °
  833.  Fshowfn ÉFX 'U╜V showfn W;F;N;T;ÉIO' 'Σ Simulate ╖W[É]'                       °
  834.   'T╜(0=ÉNC ''V'')doif ''V╜0''' 'ÉIO╜0'                                        °
  835.   'U╜r,''      '',''╖'',W,''[É]'',(╞V)╞''╖''' 'N╜1╞µF╜ÉCR W' 'N╜«∞N'           °
  836.   'N╜(N⌠'' '')ΓN' 'F╜(π''['',■N,■Γ''] ''),F'                                   °
  837.   'T╜(1<µ,V)doif ''F╜F[1╟V;]'' ''U╜''''''''''' 'U╜²1╟U,r,,F,r'                 °
  838. X 'U╜((-+/^\'' ''=ΦU)╟U),('' ╖'')[╞V],r'                                       °
  839. *(1997 7 13 12 28 58 540)                                                      °
  840.  Fsimdd ÉFX 'u╜simdd w;e' 'Σ Direct definition mode' 'u╜0'                     °
  841.   '╕((0⌠ÉNC╞w)doif ''''''Already defined'''''')/0' 'e╜''α''ε2πw'               °
  842.   'w[2]╜Γ''u╜'',''α∙ aw'' replace 2πw' 'w╜w[1 3 2]'                            °
  843. X 'w[1]╜Γε''u╜'',(e/''a ''),w[1],'' w''' 'u╜╧ÉFX w'                            °
  844. *(1992 6 3 9 59 17 424)                                                        °
  845.  Ftab ÉFX 'U╜V tab W;T;A;B;C;D;E;F;G;M;ÉPW' 'Σ Tabulate list W'                °
  846.   'T╜(0=ÉNC ''V'')doif ''V╜0''' 'M╜''Invalid data for tabulation'''            °
  847.   'V╜4╞V' 'ÉPW╜130╛30⌐G╜V[2]+79⌡V[2]=0'                                        °
  848.   'L1:╕((1<╧W)doif ''''''W╜∞0'''' ÉEA ''''W╜πW'''''')/L1'                      °
  849.   '╕(((0=µεW)δ2<µµW)doif ''U╜(~V╧4╞0)/M'')/0'                                  °
  850.   'T╜(1≥µµU╜«W)doif ''U╜πW╜(U⌠'''' '''')ΓU'''                                  °
  851.   'T╜(0<V[1])doif ''U╜(«(Φ1,╞µW)µ(V[3]µ'''' ''''),∞(╞µW)-V[3]),'''' ''''       °
  852.  ,U''' '╕(G<30)/0' 'T╜(F╜µεV[4])+C╜1╟B╜µA╜(V[3],0)╟U'                          °
  853.   'T╜⌐(1╞B)÷1⌐╛(ÉPW+F)÷T' 'U╜(E╜(V[3],C)╞U),[1](B╜T,1╟B)╞A'                    °
  854.   '''D╜εV[4]'' ÉEA ''D╜ÉAV[εV[4]+33⌡V[4]=0]''' 'L0:A╜(T,0)╟A'                  °
  855. X '╕(0=1╞µA)/0' 'U╜U,(((T+V[3]),µD)µD),E,[1]B╞A' '╕L0'                         °
  856. *(1997 7 13 12 28 59 544)                                                      °
  857.  Ftest ÉFX 'U╜V test W;P'                                                      °
  858.   'Σ Describe problem in W, (correct answer in V)' 'U╜2' 'L1:W'                °
  859.   'É╜''      ''' 'P╜æ' '''╕L0'' ÉEA ''P╜»P''' '╕(V╧P)/0'                       °
  860. X 'L0:╕(0=U╜U-1)/0' '''Incorrect. Try again''' '╕L1'                           °
  861. XCuc 1 26 ABCDEFGHIJKLMNOPQRSTUVWXYZ                                           °
  862. *(1996 4 6 16 7 37 344)                                                        °
  863.  Fvec ÉFX 'U╜vec' 'Σ Draw 2d vector in plane'                                  °
  864.   'U╜r,r,''2-        +'',r '':      °'',r,''1-    °'',r'                       °
  865. X 'U╜U,'' :  °'',r,''0|....|....|'',r,'' 0    1    2'',r'                      °
  866.