home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
teachapl.zip
/
TEACH.ZIP
/
TEACH17A.ATF
< prev
next >
Wrap
Text File
|
1997-09-19
|
58KB
|
708 lines
XNÉIO 0 1 °
XNÉCT 0 1E²13 °
XCÉFC 1 6 .,*0_² °
XNÉRL 0 887972091 °
XCÉPR 1 1 °
XCÉLX 1 5 TEACH °
XAA╜1-ÉIO-∞20 °
*(1997 8 18 12 54 27 544) °
FACC1 ÉFX 'Z╜KEYS ACC1 AMTS' 'Σ Accumulate AMTS by KEYS' °
X 'Z╜((∞µKEYS)=KEYS∞KEYS)/KEYS' 'Z╜Z,[0.5]AMTS+.⌡KEYS°.=Z' °
*(1997 8 18 12 54 32 564) °
FACC2 ÉFX 'Z╜KEYS ACC2 AMTS;P' 'Σ N⌡LOG N solution' °
'KEYS╜KEYS[Z╜√KEYS]' 'P╜²1╟1,KEYS⌠1ΦKEYS' °
X 'Z╜(P/KEYS),[0.5]+/■(+\P)ΓAMTS[Z]' °
*(1996 4 6 16 9 32 332) °
FALGORITHMS ÉFX 'ALGORITHMS' 'Σ Classification on basis of cost' °
'''PROBLEM TYPE SIZE FACTOR EXAMPLE APL2 EXPRESSION''' °
'''Constant K µA''' °
'''Logarithmic ╡N''' °
'''Linear N -A''' °
'''N(log N) N⌡╡N √A''' °
'''Quadratic N*2 A°.⌡A''' °
X '''Cubic N*3''' '''Exponential *N'',r' °
XAB╜1-ÉIO-∞10 °
*(1996 4 6 16 9 32 332) °
FBIND ÉFX 'BIND' 'Σ Binding rules of APL2' °
''' 1. Brackets bind to the left''' °
''' 2. Left arrow binds to the left''' °
''' 3. Dyadic operator binds to the right''' °
''' 4. Strand notation binding''' °
''' 5. Operator binds to its left''' °
''' 6. Function binds to its left''' °
''' 7. Function binds to its right''' °
X ''' 8. Left arrow binds to its right'',r' °
XAC╜1 2 3 4 5 °
XACOLS╜('PRODUCT')('AMOUNT')('PRICE') °
*(1997 9 14 12 30 44 504) °
XFDISCLAIMER ÉFX 'DISCLAIMER' 'Σ Copyright statement' 'disclaimer' °
*(1996 4 6 16 9 32 332) °
FDISPLAY ÉFX 'D╜S DISPLAY A;ÉIO;R;C;HL;HC;HT;HB;VL;VB;V;W;N;B' °
'Σ DISPLAY A GENERAL ARRAY IN PICTORIAL FORM' °
'Σ NORMAL CALL IS MONADIC. DYADIC CALL USED ONLY IN' °
'Σ RECURSION TO SPECIFY DISPLAY RANK, SHAPE, AND DEPTH.' 'ÉIO╜0' °
'»(0=ÉNC ''S'')/''S╜µA''' 'R╜╞µ,S Σ PSEUDO RANK.' °
'C╜''┐┌└┘'' Σ UR, UL, LL, AND LR CORNERS.' °
'HL╜''─'' Σ HORIZONTAL LINE.' °
'HC╜HL,''Θ╕'',HL,''~+ε'' Σ HORIZONTAL BORDERS.' °
'HT╜HC[(0<R)⌡1+0<╞²1╞,S]' 'ΣW╜,0╧■╞0µΓ(1⌐µA)╞A' °
'HB╜HC[3+3╛(''2⌡~A╧«A'' ÉEA ''1+╞ε0⌡(1⌐⌡/µA)╞,A'')+3⌡1<µµS]' °
'VL╜''│'' Σ VERTICAL LINE.' °
'VB╜VL,''Φ╟'' Σ VERTICAL BORDER.' °
'V╜VB[(1<R)⌡1+0<²1╞²1╟,S]' °
'»(0εµA)/''A╜(1⌐µA)µΓ╞A'' Σ SHOW PROTOTYPE OF EMPTIES.' °
'╕(1<╧A)/GEN' '╕(2<µµA)/D3' °
'D╜«A Σ SIMPLE ARRAYS.' 'W╜1╞µD╜(²2╞1 1,µD)µD' °
'N╜²1+1╟µD' '╕(0=µµA)/SS' °
'D╜(C[1],V,((W-1)µVL),C[2]),((HT,NµHL),[0]D,[0]HB,NµHL),C[0],(WµVL),C[ °
3]' '╕0' 'SS:HB╜((0 '' '')=╞0µΓA)/'' -''' °
'D╜'' '',('' '',[0]D,[0]HB,Nµ'' ''),'' ''' '╕0' °
'GEN:D╜«DISPLAY■A Σ ENCLOSED ...' 'N╜Dδ.⌠'' ''' °
'D╜(Nδ~1ΦN)≡D' 'D╜(δ≡~'' ''╤D)/D' 'D╜((1,µS)µS)DISPLAY D' °
'╕(2≥µ,S)╟D3E,0' 'D3:D╜0 ²1╟0 1╟«ΓA Σ MULT-DIMENSIONAL ...' °
'W╜1╞µD' 'N╜²1+1╟µD' °
'D╜(C[1],V,((W-1)µVL),C[2]),((HT,NµHL),[0]D,[0]HB,NµHL),C[0],(WµVL),C[ °
3]' 'D3E:N╜²2+µ,S' °
X 'V╜C[Nµ1],[0]VB[1+0<²2╟,S],[0](((²3+╞µD),N)µVL),[0]C[Nµ2]' 'D╜V,D' °
*(1996 4 6 16 9 32 332) °
FEXIT ÉFX 'EXIT' 'Σ Exit from function' '''To log off type: )OFF''' °
X '╕' °
AFN╜('Z╜KEYS ACC2 AMTS;P')('Σ N⌡LOG N solution')('KEYS╜KEYS[Z╜√KEYS]')( °
X'P╜²1╟1,KEYS⌠1ΦKEYS')('Z╜(P/KEYS),[0.5]+/■(+\P)ΓAMTS[Z]') °
*(1996 4 6 16 9 32 332) °
FGO ÉFX 'GO;T;E;B' 'Σ Expression driver' 'L0:B╜E╜''''' 'æ╜'' ''' °
'T╜æ' '╕(^/'' ''=T)/L0' '╕((^/'')OFF ''=5╞6╟T)doif ''EXIT'')/0' °
'╕(('':''εT)doif ''B╜evaldd (+/^\'''' ''''=T)╟T'')/L0' °
'''E╜ÉEM'' ÉEA T' '╕(0=µ,E)/L0' '╕B/L0' °
'''This is not a valid APL2 expression''' 'æ╜''*''' °
X '╕(''?''⌠╞1╟æ)/L0' 'E' '╕L0' °
*(1997 8 18 12 52 33 560) °
FHELP ÉFX 'HELP;N;I;T' 'Σ Help to student' '''WHAT TO DO'',r' °
''' ° To get out of the lesson''' ''' ENTER: EXIT''' °
''' ° To log off APL2''' °
''' FIRST, ENTER: EXIT THEN ENTER: )OFF''' °
''' ° To get help''' ''' ENTER: HELP''' °
''' ° When you see the prompt on a blank line''' °
''' ENTER AN APL2 EXPRESSION - OR JUST PRESS: ENTER''' 'do' °
''' ° If you get this line'',r' °
'''This is not a valid APL2 expression'',r,''*'',r' °
''' YOU CAN EITHER''' ''' A. PRESS: ENTER''' °
''' B. PRESS: ? and then ENTER to see what was incorrect''' °
''' in your expression causing that response'',r' 'do' °
'''HINTS'',r' °
'''This lesson is made up of '',(«N╜6),'' components named TEACHx, whe °
re''' '''the x stands for a digit:'',r' 'I╜0' °
'L0:T╜''TEACH'',(«I╜I+1)' 'T,'' '',1╟notb(ÉCR T)[2;]' '╕(N>I)/L0' °
'do' °
'''You may review either of these components separately. To do that,'' °
' °
'''first enter EXIT (and RETURN), then enter the name of the lesson.'' °
' '''component (e.g. TEACH4).'',r' °
'''To re-start the lesson, just enter TEACH'',r' °
'''When the screen fills up, it is a good idea to move the cursor to'' °
' '''the start of a convenient paragraph, and press ENTER'',r' °
'''You may also wish to press PAGE UP to review the prevous pages.''' °
'do' '''RESOURCES'',r' °
'''You may also enter ONE of the following words at a time'',r' °
'''ALGORITHMS Classification table of algorithms''' °
'''N TIME E Evaluating expression E N times, and timing it''' °
X 'endd' °
*(1997 9 4 12 49 1 368) °
FTEACH ÉFX 'TEACH' 'Σ Start lesson #17: APL2 by Zdenek V JIZBA' °
'exit ''TEACH''' 'initialize' 'TEACH1 Σ Solving problems with APL2' °
'TEACH2 Σ Function TEST to time APL2 expressions' °
'TEACH3 Σ Classification of algorithms' °
'TEACH4 Σ Strategies for efficient problem solving' °
'TEACH5 Σ Format by specification' °
X 'TEACH6 Σ Format by example; format control' 'problems' °
*(1997 7 11 12 24 35 424) °
FTEACH1 ÉFX 'TEACH1;A;B' °
'Σ Discus how in APL there are many ways to solve a problem' °
'''In lesson 16 we investigated ways to use software that has been''' °
'''developed in other workspaces. When designing a new application,''' °
'''one not only should take advantage of previous work, but one should °
''' °
'''also try to define APL2 functions that are fast and efficient.'',r' °
'''In this lesson we will explore the issue of optimal resource''' °
'''utilization. A great deal of the material presented in this lesson' °
'' '''is based on lectures and publications by Roy A. Sykes Jr.'',r' °
'do' ''' SOLVING PROBLEMS WITH APL2'',r' °
'''When experimenting with APL, one soon discovers that often there''' °
'''are many ways to solve any one particular problem. Consider the''' °
'''following simple example: We have two numeric vectors, A and B'',r' °
'show ''A╜?5µ10'' ''B╜?5µ10''' °
'''Now, suppose we desire to find the sum of the product of A and B.'' °
,r' 'show ''+/A⌡B'' ''A+.⌡B''' °
'''Which of these two is preferable? Are there still other ways to''' °
'''produce the same result?''' 'do' °
'''The answer to these question lies partially in personal preference, °
''' °
'''but more importantly on the nature of the problem. Suppose that''' °
'''instead of vectors, A and B are arrays:'',r' °
'show ''A╜?3 3µ10'' ''B╜?3 3µ10''' °
'''Will we still get the same results with the two expressions?''' °
'do' 'show ''+/A⌡B'' ''A+.⌡B''' °
'''Clearly at least one of these two expressions is incorrect. When'' °
' °
'''selecting an expression, it seems like a good idea to pick the one' °
'' °
'''that will produce valid results when generalized to higher order''' °
'''arrays.''' 'do' °
'''Another criterion for selecting a particular expression is the amou °
nt''' °
'''of time it takes the computer to find an answer. We have already s °
een''' °
'''one example of this in lesson 2. The average can be obtained with: °
''' °
''' (+/∙)÷µ∙ or with +/∙÷µ∙. The second expression is shorter, bu °
t''' °
'''it will clearly take more computer time, and be less accurate for l °
ong''' '''vectors because of roundoff errors.''' 'do' °
'show ''A╜3 3µ∞9'' ''+/φA'' ''+≡A'' ''1¥A''' °
'''The three expressions are different, yet they give the same answer. °
''' '''(Can you explain why the last one works?)'',r' °
'''To find out which of these three expressions is more efficient,we'' °
' °
'''will use the System Function TIME STAMP (ÉTS), in function TIME.'', °
r' '1 showfn ''TIME''' 'do' °
'''Function TIME evaluates the APL2 expression in the right argument.' °
'' °
'''It evaluates it the number of times specified in the left argument, °
''' °
'''and reports the average number of miliseconds for its execution.''' °
'''(The overhead of the »■ is included in this estimate.)'',r' °
'show ''100 TIME ''''+/φA'''''' ''100 TIME ''''+≡A'''''' ''100 TIME '' °
''1¥A''''''' °
'''A reduction is certainly better than decode, but adding a transpose °
''' °
'''increases the number of primitive functions to two. In the second'' °
' °
'''expression only one primitive function is applied, namely +, and''' °
'''therefore this produces the best result.''' 'do' °
'''Testing of the efficiency of APL2 expressions is a good idea, but'' °
' °
'''it may not lead to the best or most efficient code. If an algorithm °
''' °
'''is to be used heavily, then it may still be desirable to design an' °
'' °
'''object code by compilation of a FORTRAN or C source program. (These °
''' °
'''can be linked to APL2 using Name association and Processor 11.)''' °
'do' °
'''Certain commonly used idioms are recognized by the APL2 interpreter °
''' °
'''and their evaluation is optimized. Therefore applying function TIME °
''' °
'''when experimenting with alternate solutions is highly recommended,' °
'' '''especially when developing applications for heavy usage.'',r' °
'''One way to sharpen your APL skills is to try to find as many differ °
ent''' °
'''solutions to a problem as possible. Roy Sykes has published many s °
uch''' '''problems. Here is one example:'',r' °
'''Write as many different expressions for turning a numeric vector V °
into''' '''a one column matrix. One solution would be'',r' °
'show ''φ(1,µV)µV╜∞4''' °
'''Can you think of other expressions that will produce the same resul °
t?''' 'do' °
'''One rule of thumb used by Roy Sykes is to count the number of''' °
'''primitive functions invoked in the expression. The fewer this numbe °
r''' °
'''the more efficient the evaluation. (This rule of thumb does not''' °
X '''always work as we have seen in the example above.)''' 'endd' °
*(1997 7 11 12 53 21 484) °
FTEACH2 ÉFX 'TEACH2;TEST;T' 'Σ Testing TIME' °
'''Let us take another look at Time Stamp. It turns out that it is not °
''' °
'''a very reliable measurement of time. Although the results of the''' °
'''experiment with function TIME seem reasonably valid, we must enter' °
'' °
'''a note of caution. Let us define function TEST to evaluate the inte °
rnal''' '''consistency of ÉTS'',r' °
'FN╜''U╜TEST W;T'' ''Σ Evaluate performance of ÉTS''' °
'FN╜FN,''U╜∞0'' ''L0:T╜60 60 1000¥²3╞ÉTS''' °
'T╜ÉFX FN,Γ''U╜U,(60 60 1000¥²3╞ÉTS)-T ╪ ╕(0<W╜W-1)/L0''' °
'''Function TEST should measure the smallest time interval between''' °
'''consecutive readings of ÉTS'',r' '1 showfn ''TEST''' °
'''The results will of course depend on your specific hardware, but''' °
'''will also give you some idea of the accuracy of results with functi °
on''' °
'''TIME. Function TEST should produce a zero most of the time because' °
'' '''it takes less than 10 miliseconds to evaluate ÉTS.'',r' °
'show ''TEST 10''' °
'''Next we will try TEST 1000 times (PRESS ENTER and NOTE the DELAY)'' °
' 'do' 'show ''+/TEST 1000''' °
'''In general, you should use TIME on problems that take a significant °
''' °
'''interval of time to complete, and repeat the test more than once to °
''' °
'''get a feel for the probable error. The time values are not absolute °
''' °
'''because they include the overhead in function TIME. Furthermore, th °
e''' °
'''comparisons should be made using the same values in the left''' °
'''argument of TIME. (Why?)''' 'do' °
'''With these caveats, let us look at the question of the time it take °
s''' °
'''to solve problems on a computer. (Most of this section has been tak °
en''' °
'''from lecture notes of a free IBM seminar on APL2. The specific''' °
'''lecture is that given by Roy A. Sykes on Optimized APL2.)''' 'do' °
'''There are some computer problems that are inherently time consuming °
.''' °
'''Given a specific computer architecture, and a given problem, there °
is''' °
'''always a lower limit of time, it will take a computer to reach a''' °
'''solution. For a given problem, this calculation time is in some way °
''' °
'''related to the amount of data to be processed. It makes sense to sa °
y''' °
'''that the expression +/∞1000 will take longer to evaluate than the'' °
' '''expression +/∞10'',r' °
'show ''10 TIME ''''+/∞500'''''' ''10 TIME ''''+/∞5''''''' °
'''If we denote the size of the problem by N, then we can classify an' °
'' °
'''algorithm in terms of how N changes the solution time. The followin °
g''' '''table summarizes these relations'',r' 'ALGORITHMS' °
X '''Let us take a closer look at some of these algorithms''' 'endd' °
*(1997 7 11 13 6 34 352) °
FTEACH3 ÉFX 'TEACH3' 'Σ Examples of computation time' °
'''Before we show examples of these types of problems, there is a cave °
at.''' °
'''Some of the actual sample timings may not follow the predicted''' °
'''duration because of optimizations by the APL2 interpreter.''' 'do' °
'''Problems that can be solved in roughly the same amount of time''' °
'''regardless of the data size fall under the category of CONSTANT.''' °
'''A typical example of this type of problem is taking the shape of''' °
'''an array (µA). Since in APL2 the dimensions of an array are stored' °
'' °
'''as part of the data structure, no processing is required other than °
''' '''the extraction of the information from a known location. The''' °
'''algorithm first finds the rank, which then determines the location' °
'' '''and size of the shape.''' 'do' °
'''Problems dealing with a process called binary search are solved in' °
'' °
'''LOGARITHMIC time. This means that if the data size is doubled, the' °
'' °
'''time to solve the problem increases by a factor proportional to the °
''' °
'''logarithm of two. There are no simple examples of this type of''' °
'''problem in APL2.''' 'do' °
'''In some problems, the time to reach a solution is directly related' °
'' °
'''to the size of the problem. This type of problem is called LINEAR'' °
,r' °
'show ''100 TIME ''''-∞10'''''' ''100 TIME ''''-∞50'''''' ''100 TIME ' °
'''-∞250''''''' °
'''(By extrapolating to a vector of length 0 you can estimate the''' °
'''overhead for this particular algorithm.)''' 'do' °
'''In some problems the time to reach a solution increases faster than °
''' °
'''the data length, but less than the square of the length. These are' °
'' '''called the N LOG N type problems'',r' °
'show ''A╜?500µ10000'' ''100 TIME ''''√5╞A'''''' ''100 TIME ''''√50╞A' °
'''''' 'show ''100 TIME ''''√500╞A''''''' °
'''Problems that require four times the time when the data size is''' °
'''doubled are called QUADRATIC. The outer product is one example of'' °
' '''such problems'',r' °
'show ''C╜5╞B╜10╞A╜∞20'' ''100 TIME ''''C°.⌡C'''''' ''100 TIME ''''B°. °
⌡B''''''' 'show ''100 TIME ''''A°.⌡A''''''' °
'''There is a simple APL2 example of a cubic type of problem, but we h °
ave''' °
'''not yet introduced the primitive function associated with it.'',r' °
'''The most difficult problems to solve are those for which computatio °
n''' °
'''time increases exponentially. One example of such problems would be °
''' °
'''the game of chess, where the quality of game is related to the numb °
er''' '''of moves ahead to be evaluated.''' 'do' °
'''What conclusion can we draw from this discussion?'',r' °
'''For one it is ALWAYS IMPORTANT to study alternate methods for solvi °
ng''' °
'''a given problem. If there are two algorithms, one being LINEAR, and °
''' °
'''another being QUADRATIC, the linear one is preferable. Consideratio °
n''' °
'''of the nature of the algorithm may be far more important than any'' °
X' '''other factor in determining its utility.''' 'endd' °
*(1997 7 11 13 17 36 404) °
FTEACH4 ÉFX 'TEACH4;PROD;KEYS;AMTS' °
'Σ Strategies for efficient problem solving' °
'''Earlier we mentioned that for every problem, there is a minimum tim °
e''' °
'''under optimal conditions. Assuming we have selected an optimal''' °
'''algorithm, APL2 does not always solve problems in this minimal time °
.''' °
'''If we were to take the ratio of (minimal time)/(APL2 time), then we °
''' °
'''might find values anywhere between 0 and near 1.0. Let us call this °
''' '''ratio the APL2 EFFICIENCY.''' 'do' °
'''What types of strategies should be used to improve the efficiency o °
f''' °
'''APL2 computation? On some problems it really does not matter much.' °
'' °
'''If we get a solution in 1/10 second with efficiency of 0.1, who car °
es?''' °
'''On the other hand, if it takes 60 minutes to solve a problem with'' °
' °
'''a 0.9 efficiency, then any improvement in speed will be welcome. In °
''' °
'''such a case it is definitely recommended to investigate compiled co °
de.''' 'do' °
'''However even within APL2 it is the SELECTION of the best algorithm' °
'' °
'''that will determine the quality of performance. There is no substit °
ute''' °
'''to GOOD PROGRAMMING, no matter what computer language is used.'',r' °
'''The following example is taken from Roy A. Sykes notes.''' 'do' °
'''In business, products on sale are identified by numeric keys. Suppo °
se''' '''that a store sells 4 products with the following keys:'',r' °
'PROD╜201 346 537 664' 'show ''PROD''' °
'''At the end of the day, when the sales from cash registers are''' °
'''recorded, we may have the following results'',r' 'KEYS╜PROD[?5µ4]' °
'AMTS╜?5µ9' 'show ''KEYS'' ''AMTS''' °
'''We need an algorithm to add all AMTS associated with the same key.' °
'' '''Here is one APL2 function to do that.'',r' °
'show ''FN╜''''Z╜KEYS ACC1 AMTS'''' ''''Σ Accumulate AMTS by KEYS''''' °
'' 'show ''FN╜FN,Γ''''Z╜((∞µKEYS)=KEYS∞KEYS)/KEYS''''''' °
'''This expression is an idiom to identify the unique elements of KEYS °
.'',r' 'show ''FN╜FN,Γ''''Z╜Z,[0.5]AMTS+.⌡KEYS°.=Z''''''' °
'''The outer product at right KEYS°.=Z generates a boolean array. The' °
'' °
'''inner product of this array with AMTS produces the desired sums.''' °
'''these are then catenated to the unique elements of KEYS.''' 'do' °
'show ''ÉFX FN''' '1 showfn ''ACC1''' 'show ''KEYS ACC1 AMTS''' °
'''Function ACC1 is neat, but since it uses dot products, it is an''' °
'''N SQUARE algorithm. As the number of KEYS doubles, the time to''' °
'''solve the problem increases by four. We can do much better by''' °
'''defining an N⌡LOG N algorithm.'',r' °
'show ''FN╜''''Z╜KEYS ACC2 AMTS;P'''' ''''Σ N⌡LOG N solution''''''' °
'show ''FN╜FN,''''KEYS╜KEYS[Z╜√KEYS]'''' ''''P╜²1╟1,KEYS⌠1ΦKEYS''''''' °
'''First we sort KEYS in increasing order (N⌡LOG N). This collects''' °
'''all like keys together. Then we generate boolean vector P giving''' °
'''the boundaries of changes in keys.'',r' °
'show ''FN╜FN,Γ''''Z╜(P/KEYS),[0.5]+/■(+\P)ΓAMTS[Z]''''''' °
'''Using the partition function we cluster AMTS for like keys and''' °
'''add by groupings.''' 'do' 'show ''ÉFX FN''' '1 showfn ''ACC2''' °
'show ''KEYS ACC2 AMTS''' °
'''We can see how these two functions behave'',r' °
'show ''10 TIME ''''KEYS ACC1 AMTS'''''' ''10 TIME ''''KEYS ACC2 AMTS' °
'''''' '''As we increase the size of arrays'',r' °
'show ''KEYS╜PROD[?100µ4]'' ''AMTS╜?100µ10'' ''KEYS'' ''AMTS''' °
'''We can see what effect this has on execution speed'',r' °
'show ''10 TIME ''''KEYS ACC1 AMTS'''''' ''10 TIME ''''KEYS ACC2 AMTS' °
X'''''' 'endd' °
*(1997 7 11 13 31 45 496) °
FTEACH5 ÉFX 'TEACH5;HDG;INVOICE' 'Σ Dyadic Format' °
''' DYADIC FORMAT'',r' °
'''In many applications, particularly in business, it is not the''' °
'''amount of computer time it takes to solve a problem, but the''' °
'''ease of generating a report that matters.'',r' °
'''A computer user today expects to be able to see some sort of a''' °
'''graphical display in color. Although APL2 is not designed to do''' °
'''graphical processing, Auxilliary Processors (invoked by System''' °
'''functions ÉSV*), or non-APL2 software (accessed by ÉNA), provide''' °
'''the needed interface to support just about any demand.'',r' °
'''The dyadic version of the format primitive supports some of the nee °
ds''' '''in tabulation of data.''' 'do' °
'''There are two distinct ways to use the dyadic format primitive:''' °
'''In format by specification, the left argument is a simple integer'' °
' '''vector. In format by example, the left argument is a simple''' °
'''character vector.''' 'do' '''FORMAT BY SPECIFICATION'',r' °
'''Suppose we have a numeric array called INVOICE:'',r' °
'show ''INVOICE╜2 3µ10432 5 34.75 10446 10 55.95'' ''INVOICE''' °
'''If the three columns are called PRODUCT, AMOUNT and PRICE''' °
'''we can generate the following table'',r' °
'show ''COLS╜''''PRODUCT'''' ''''AMOUNT'''' ''''PRICE'''''' ''COLS,[1] °
INVOICE''' °
'''Obviously, this would not be a very acceptable display. We can''' °
'''control the spacing and number of decimal points of numeric''' °
'''data with format by specification'',r' °
'show ''8«INVOICE'' ''8 2«INVOICE'' ''12 0 9 0 10 2«INVOICE''' °
'''In the first example we specified 8 decimal points for each''' °
'''number in the array. In the second example we limited decimal''' °
'''fraction to only two digits, but specified eight positions for''' °
'''each number. In the third example we reserved twelve columns for''' °
'''PRODUCT as an integer (12 0), nine columns for AMOUNT as an integer °
''' °
'''(9 0), and 10 columns for price with two decimal digits (10 2).''' °
'do' '''But now, we no longer can catenate the headers as before'',r' °
'show ''HDG╜ε12 9 10╞■''''PRODUCT'''' ''''AMOUNT'''' ''''PRICE''''''' °
'show ''HDG,[1]12 0 9 0 10 2«INVOICE''' °
'''This still does not look good. It is because the head labels are''' °
'''left adjusted, while the numeric columns are right adjusted. We''' °
'''can correct that with'',r' °
'show ''HDG╜ε²12 ²9 ²10╞■''''PRODUCT'''' ''''AMOUNT'''' ''''PRICE''''' °
'' 'show ''HDG,[1]12 0 9 0 10 2«INVOICE''' °
'''This looks much better. In summary, in format by specification''' °
'''the left argument can be a scalar integer, or an integer vector.''' °
'''A single integer specifies the number of decimal points to be''' °
'''displayed. A vector of length two specifies the number of columns'' °
' '''(width of the field), and the number of decimal points for all''' °
'''columns in the array. Finally a vector of length 2⌡²1╞µARRAY can''' °
X '''supply the width and number of decimals for each column.''' 'endd' °
*(1997 7 12 13 29 41 476) °
FTEACH6 ÉFX 'TEACH6;INVOICE;no;ds;at;PIC;ÉFC' 'Σ Format by example' °
'ÉFC╜''.,*0_-''' '''FORMAT BY EXAMPLE'',r' °
'INVOICE╜2 3µ10432 5 34.75 10446 10 55.95' °
'''One problem with format by specification is its limitation to APL2' °
'' °
'''symbols. In format by example, the left argument is a character''' °
'''vector describing the way each row of the table should look like.'' °
,r' °
''' In business applications, it is common to see special symbols''' °
'''(called decorations) associated with tabular data. Some of these''' °
'''symbols cannot be readily obtained from the keyboard. They can be'' °
' '''extracted from the atomic vector.'',r' °
'show ''ÉAV[36 37 65]'' ''(no ds at)╜ÉAV[36 37 65]''' °
'''Variables "no" "ds" and "at", can then be used to design a characte °
r''' °
'''vector that can be used as a pattern for displaying tabular data.'' °
,r' °
'show ''PIC╜no,''''55555 55'''',at,'''' '''',ds,''''555.50 EA''''' °
' ''PIC''' °
'''Vector PIC represents a picture of the desired format. It describes °
''' '''how the numeric information is to be formatted'',r' °
'show ''PIC«INVOICE''' ''' FORMAT CONTROL'',r' °
'''Before we describe the conventions used in format by example, we mu °
st''' °
'''introduce a System Variable called FORMAT CONTROL (ÉFC). This varia °
ble''' °
'''defines the symbols that are to be used in displaying numeric data °
in''' '''conjunction with format by example.'',r' 'show ''ÉFC''' °
'''ÉFC[1] *Character to be used as decimal point''' °
'''ÉFC[2] Character to mark thousands position''' °
'''ÉFC[3] Fill for blanks (see below on usage of digit 8)''' °
'''ÉFC[4] *Fill for numeric overflow (avoiding DOMAIN ERROR)''' °
'''ÉFC[5] Character to be printed as blank''' °
'''ÉFC[6] *Negative number indicator'',r' °
'''The elements marked with * are also used in format by specification °
.''' °
'''The first two elements of ÉFC are needed, because in some European' °
'' °
'''countries the decimal point and comma for thousands are interchange °
d''' °
'''We will first illustrate how ÉFC can be used to modify a tabulation °
.''' °
'''Then we will discuss the specifications of the left argument of''' °
'''format by example.''' 'do' °
'''When a number is greater than the allowed space, APL2 normally woul °
d''' °
'''cause an interrupt. However, when using Format by Example, this''' °
'''interrupt need not ocurr; the space provided for that number can''' °
'''be filled out by a special character in ÉFC[4]. When ÉFC[4] is 0,'' °
' '''there will be an interrupt and an error message.'',r' °
'show ''ÉFC[4]'' ''TEST╜2 2µ²1 1234.56789 200 4'' ''''''55 555.50''''« °
TEST''' °
'''This interrup can be avoided if ÉFC[4] is assigned a "replacement"' °
'' '''character'',r' °
'show ''ÉFC[4]╜''''?'''''' ''''''55 555.50''''«TEST''' °
'''In some European countries, the comma and period have their functio °
n''' '''reversed'',r' °
'show ''ÉFC[1 2]╜ÉFC[2 1]'' ''''''555 5,555.50''''«TEST''' °
'ÉFC[1 2]╜ÉFC[2 1]' ''' ILLUSTRATIONS OF FORMAT BY EXAMPLE'',r' °
'''Let us take another look at PIC'',r' 'show ''PIC''' °
'''This character string contains two types of symbols. Digits, and''' °
'''elements of ÉFC[1 2] are called CONTROL CHARACTERS. These specify'' °
' °
'''where and how numbers should be displayed. All other characters in' °
'' °
'''PIC will be displayed as they are but with location conditionally'' °
' °
'''modified by the control characters. In the examples below, certain' °
'' °
'''control characters work only with numeric vectors. Let us see how'' °
' '''use various digits'',r' °
'''5 Normal formatting rules of APL2'',r' °
'show ''''''55 5555.55''''«TEST''' '''0 Pad with zeros'',r' °
'show ''''''0555 0005.5000''''«TEST''' °
'''1 Float the symbol or decorator only against negative number'',r' °
'show ''(''''-551 5555.0'''')«TEST''' °
'''2 Float the decorator only against positive number'',r' °
'show ''''''+552 5555.50''''«TEST''' °
'''3 Float the decorator against all numbers'',r' °
'show ''(ds,''''5553.5 -513 '''')«ΦTEST''' °
'''4 Counteract the effect of 1, 2 or 3'',r' °
'show ''''''-551.20CR''''«²1 10 ²100'' ''''''-551.40CR ''''«²1 10 ²100 °
''' '''6 End of field. Display decorator to the right'',r' °
'show ''''''0006/06/06 06:06''''«5╞ÉTS''' °
'''7 Use scientific notation'',r' °
'show '''''' -1.70E-1''''«2.5 25.3''' °
'''8 Fill empty portions of field with ÉFC[3]'',r' °
'show ''ÉFC[3]'' ''''''-1888 8555.50''''«TEST''' °
'''9 Pad with 0 the position of 9'',r' °
X 'show ''''''-9915 9995.50''''«TEST''' 'endd' °
XNTEST 2 2 2 ²1 1234.56789 200 4 °
*(1997 7 11 12 13 12 288) °
FTIME ÉFX 'U╜V TIME W;T' 'Σ Measure execution time of W' °
X 'U╜60 60 1000¥²3╞ÉTS' 'T╜»■VµΓW' 'U╜(,-U-60 60 1000¥²3╞ÉTS)÷V' °
XAV╜1 2 3 4 °
*(1997 7 13 12 28 49 504) °
Faddquote ÉFX 'u╜addquote w' °
'Σ Put quotes around a string, and double existing quotes' °
X 'u╜ÉAV[40],((1+w=ÉAV[40])/w),ÉAV[40]' °
*(1997 7 24 13 20 38 476) °
Fav ÉFX 'av;A;N;I;ÉIO' 'Σ Display characters in ÉAV' 'ÉIO╜0' °
'A╜22 78µ'' ''' 'N╜3 0«φ12 22µ1+∞356' 'A[;,(6⌡∞12)°.+2 3 4]╜N' °
'A[;6+6⌡∞12]╜φ12 22µÉAV' 'ΣA[8 10 13;6]╜'' ''' 'A[13;6]╜'' ''' °
X 'A[14+∞8;68 69 70 72]╜'' ''' 'A' °
*(1991 11 11 8 25 13 316) °
Fdate ÉFX 'u╜date w' 'Σ Format date and time of day' 'u╜«6╞w' °
X 'u╜('' ''⌠u)Γu' 'u╜εu,■''-- .. ''' °
XCdig 1 10 1234567890 °
*(1997 9 9 13 0 45 372) °
Fdisclaimer ÉFX 'disclaimer' 'Σ Copyright statement' °
'(10µ'' ''),''Copyright, Z. V. Jizba, 1995,1996,1997'',r' °
''' This and subsequent workspaces labelled TEACHxx are made available °
''' °
'''at no cost to anyone who desires to learn how to use effectively''' °
'''the IBM/OS2 version of APL2.'',r' °
'''This software is provided "AS IS" with no WARRANTY of any kind, eit °
her''' °
'''express or implied. Any risk in its use resides with you, the user °
of''' '''these tutorials.'',r' ''' ACKNOWLEDGEMENTS'',r' °
''' In writing these tutorials, I am greatly indebted to Roy Sykes, wh °
ose''' °
'''excellent lectures increased my understanding of the language.''' °
'''Discussions with the late Harry Bertucelli clarified a number of''' °
'''concepts and caused me to change some improper terminology that was °
''' °
'''used in previous versions of these tutorials. Mr. Benjamin Archer'' °
' °
'''kindly checked out a nearly final version, bringing to my attention °
''' '''some ommisions, misspellings, and invalid terminology.'',r' °
X '''(PRESS ENTER to continue)''' °
*(1997 7 13 12 28 50 508) °
Fdo ÉFX 'do;T;E' 'Σ Expression driver' 'E╜''''' 'æ╜'' ''' 'T╜æ' °
'╕(^/'' ''=T)/0' °
'╕(('':''εT)doif ''B╜evaldd (+/^\'''' ''''=T)╟T'')/2' °
'''E╜ÉEM'' ÉEA T' '╕(0=µ,E)/2' °
'''This is not a valid APL2 expression''' 'æ╜''*''' '╕(''?''⌠╞1╟æ)/2' °
X 'E' '╕2' °
*(1997 7 13 12 28 50 508) °
Fdoif ÉFX 'U╢╜V╢ doif W╢;t╢' 'Σ Rule' '╕(^/~U╢╜V╢)/0' °
X '''U╢╜V╢ doif■ W╢'' ÉEA ''»V╢/W╢''' °
*(1997 9 9 12 50 14 444) °
Fendd ÉFX 'endd' 'Σ end of special feature' '20µ''²'' ╪ ╕(4<µÉLC)/0' °
X 'do' °
*(1997 8 18 12 50 27 528) °
Ferase ÉFX °
'erase;t;EXIT;GO;HELP;DISPLAY;BIND;REVIEW;RULE;TIME;ALGORITHMS;DISCLAI °
MER' 'Σ Erase all global functions and variables' 't╜ÉNL 3' °
't╜(~t^.εlc,'' '')≡t' 't╜ÉEX(~t[;∞5]^.=''TEACH'')≡t' 't╜ÉNL 2' °
X 't╜ÉEX(~t^.εlc,'' '')≡t' 't╜ÉNL 4' 't╜ÉEX(~t^.εlc,'' '')≡t' °
*(1997 7 27 13 47 41 608) °
Fevaldd ÉFX 'u╜evaldd w;c;n' 'Σ Evaluate direct definition' 'u╜0' °
'n╜(w∞''Σ'')-1' 'c╜(((n╞w)⌠'':'')Γn╞w),Γ''ΣDD '',(n+1)╟w' °
'╕((1 label╞c)doif ''''''Invalid label'''''')/0' °
'╕((2=µc)doif ''u╜showdd 1╙c'')/0' °
'╕((3=ÉNC╞c)doif ''u╜⌡µÉ╜(╞c),'''' is already defined.'''''')/0' °
'╕((3=µc)doif ''u╜simdd c'')/0' 'c╜(Γ''α∙ aw'')replace■c' °
'u╜ε''u╜'',((''a''εεc[2 3 4])/''a ''),(╞c),'' w;t;b''' °
'u╜u(5πc)(''b╜(t╜'',(3πc),'')/'',addquote ''u╜'',4πc)' °
X 'u╜u,''╕(t doif b)/0''(''u╜'',2πc)' 'u╜╧ÉFX u' °
*(1997 7 25 13 27 52 564) °
Fexit ÉFX 'V exit W;T' 'Σ Exit if too many suspended functions' °
'╕(0⌠ÉNC ''V'')/L0 ╪ V╜10' 'L0:╕(V>µÉLC)/0' °
'''There are too many suspended functions''' '''Please enter '',W' °
X '╕' °
*(1997 7 26 12 33 39 536) °
Fget ÉFX 'U╜V get W;t;T;ÉPR' 'Σ Prompt for response from keyboard' °
'ÉPR╜T╜ÉAV[ÉIO+255] ╪ ╕(0⌠ÉNC ''V'')/L0 ╪ V╜1' 'L0:V╜V╧1' 'æ╜W ╪ t╜æ' °
'U╜(+/^\t=T)╟t' '╕(''╕''⌠╞U)/L1 ╪ ╕' 'L1:╕V/0' 't╜(U⌠'' '')ΓU' °
X 'U╜(µt),(ΓU),t' °
*(1997 7 28 13 33 8 424) °
Fglobals ÉFX 'globals' 'Σ Initialize useful global variables' °
'uc╜''ABCDEFGHIJKLMNOPQRSTUVWXYZ''' °
'lc╜''abcdefghijklmnopqrstuvwxyz''' 'dig╜''1234567890''' °
X 'r╜ÉAV[13+ÉIO]' 'q╜''''''''' °
*(1997 7 3 12 47 6 368) °
Finitialize ÉFX 'initialize;T' 'Σ Initialize workspace' °
'''AT ALL TIMES, TO CONTINUE, PRESS RETURN!'',r' °
'''To see disclaimers enter:'',r,'' disclaimer''' 'do' 'erase' °
'globals' °
'''Make sure the CAP LOCK light on your keyboard (upper right) is ON!' °
X'' 'endd' °
*(1997 7 27 13 14 33 444) °
Flabel ÉFX 'u╜v label w' °
'Σ Return 1 if label w does not begin with a cap' °
'╕(0⌠ÉNC ''v'')/L0 ╪ v╜0' 'L0:v╜v╧1 ╪ w╜εw ╪ ╕v/L1 ╪ ╕(u╜0⌠ÉNC w)/0' °
X 'L1:╕(u╜~^/wεlc,uc,dig)/0' 'u╜w[1]εlc,dig' °
XClc 1 26 abcdefghijklmnopqrstuvwxyz °
*(1997 7 13 12 28 55 528) °
Fnon ÉFX 'non;T;RC;ET;R' 'Σ Ignore keyboard entry' 'æ╜'' ''' 'T╜æ' °
'╕(0=µ(T⌠'' '')/T)/0' '(RC ET R)╜ÉEC T' '╕(0=RC)/2' °
X '╕((1=RC)doif ''R'')/2' '╕2' °
*(1997 7 13 12 28 55 528) °
Fnotb ÉFX 'u╜notb w' 'Σ Remove trailing blanks' °
'╕((1<╧w)doif ''u╜notb■ w'')/0' '╕((1<µµw)doif ''u╜πnotb Γ[2]w'')/0' °
X 'u╜(1-(,'' ''⌠Φw)∞1)╟w' °
*(1997 7 27 12 55 6 496) °
Fproblems ÉFX 'problems' 'Σ Problems' °
'''That is all for this lesson. Remember, if you want to practice,''' °
'''and plan to use direct definitions, be sure to first enter GO.''' °
'''Direct definitions will then be accepted. To exit GO, enter EXIT.'' °
,r' °
'''To erase a previously defined DIRECT DEFINITION FUNCTION, enter'',r °
' ''' )ERASE functionname'',r' °
X '''WARNING! do not use )ERASE on other labels.'',r' °
XCq 0 ' °
XCr 0 °
*(1997 7 13 12 28 56 532) °
Freplace ÉFX 'u╜v replace u;i;r;s' 'Σ Replace elements in v in u' °
'i╜Γ∞µu' 's╜2πv╜(v⌠'' '')Γv' 'i╜⌡r╜i⌡■Γ[1]u°.=╞v' °
X 'u[(εi)/εr]╜s[(εi)/εi⌡■∞µs]' °
*(1997 7 13 12 28 56 532) °
Fround ÉFX 'U╜V round W' 'Σ Half adjust to V th decimal' °
X 'U╜(╛0.5+W⌡10*V)÷10*V' °
*(1997 7 13 12 28 57 536) °
Fshow ÉFX '╢V show ╢W;╢T;╢B' 'Σ Display and execute ╢W' °
'╢T╜(0=ÉNC ''╢V'')doif ''╢V╜0''' °
'╕((0=╧╢W)doif ''show ╢W,'''' '''''')/0' °
'╕((1<╧╢W)doif ''╢V show ■╢W'')/0' ''' '',╢W' °
X '╕((╢V^'':''ε╢W)doif ''╢T╜evaldd ╢W'')/L0' '''ÉEM'' ÉEA ╢W' 'L0:do' °
*(1997 7 13 12 28 57 536) °
Fshowdd ÉFX 'u╜showdd w;a;b;c;r' °
'Σ Display a direct definition function' °
'╕((1=╧w)doif ''u╜showdd Γw'')/u╜0' °
'╕((3⌠ÉNC╞w)doif ''(ε╞w),'''' is not a function'''''')/0' °
'c╜Γ[2]ÉCR╞w' 'c╜notb(2╞c),(Γ''aw α∙'')replace■2╟c' °
'╕((~''ΣDD''╧3╞2πc)doif ''''''Not a direct definition function'''''')/ °
0' 'u╜1' 'b╜('' ''⌠╞c)Γ╞c' 'a╜'' ''' 'r╜2╟3πc' °
'╕((3=µc)doif ''a,(╞w),'''':'''',r,(3<µ2πc)/'''' Σ'''',3╟2πc'')/0' °
'a╜a,(╞w),'':'',(2╟5πc),'':''' 'b╜(+\r=''('')-+\r='')''' 'b╜b∞0' °
X 'a╜a,(²3╟(b-1)╞3╟r),'':'',2╟»(b+2)╟r' 'a,(3<µ2πc)/'' Σ'',3╟2πc' °
*(1997 7 13 12 28 57 536) °
Fshowfn ÉFX 'U╜V showfn W;F;N;T;ÉIO' 'Σ Simulate ╖W[É]' °
'T╜(0=ÉNC ''V'')doif ''V╜0''' 'ÉIO╜0' °
'U╜r,'' '',''╖'',W,''[É]'',(╞V)╞''╖''' 'N╜1╞µF╜ÉCR W' 'N╜«∞N' °
'N╜(N⌠'' '')ΓN' 'F╜(π''['',■N,■Γ''] ''),F' °
'T╜(1<µ,V)doif ''F╜F[1╟V;]'' ''U╜''''''''''' 'U╜²1╟U,r,,F,r' °
X 'U╜((-+/^\'' ''=ΦU)╟U),('' ╖'')[╞V],r' °
*(1997 7 13 12 28 58 540) °
Fsimdd ÉFX 'u╜simdd w;e' 'Σ Direct definition mode' 'u╜0' °
'╕((0⌠ÉNC╞w)doif ''''''Already defined'''''')/0' 'e╜''α''ε2πw' °
'w[2]╜Γ''u╜'',''α∙ aw'' replace 2πw' 'w╜w[1 3 2]' °
X 'w[1]╜Γε''u╜'',(e/''a ''),w[1],'' w''' 'u╜╧ÉFX w' °
*(1992 6 3 9 59 17 424) °
Ftab ÉFX 'U╜V tab W;T;A;B;C;D;E;F;G;M;ÉPW' 'Σ Tabulate list W' °
'T╜(0=ÉNC ''V'')doif ''V╜0''' 'M╜''Invalid data for tabulation''' °
'V╜4╞V' 'ÉPW╜130╛30⌐G╜V[2]+79⌡V[2]=0' °
'L1:╕((1<╧W)doif ''''''W╜∞0'''' ÉEA ''''W╜πW'''''')/L1' °
'╕(((0=µεW)δ2<µµW)doif ''U╜(~V╧4╞0)/M'')/0' °
'T╜(1≥µµU╜«W)doif ''U╜πW╜(U⌠'''' '''')ΓU''' °
'T╜(0<V[1])doif ''U╜(«(Φ1,╞µW)µ(V[3]µ'''' ''''),∞(╞µW)-V[3]),'''' '''' °
,U''' '╕(G<30)/0' 'T╜(F╜µεV[4])+C╜1╟B╜µA╜(V[3],0)╟U' °
'T╜⌐(1╞B)÷1⌐╛(ÉPW+F)÷T' 'U╜(E╜(V[3],C)╞U),[1](B╜T,1╟B)╞A' °
'''D╜εV[4]'' ÉEA ''D╜ÉAV[εV[4]+33⌡V[4]=0]''' 'L0:A╜(T,0)╟A' °
X '╕(0=1╞µA)/0' 'U╜U,(((T+V[3]),µD)µD),E,[1]B╞A' '╕L0' °
*(1997 7 13 12 28 59 544) °
Ftest ÉFX 'U╜V test W;P' °
'Σ Describe problem in W, (correct answer in V)' 'U╜2' 'L1:W' °
'É╜'' ''' 'P╜æ' '''╕L0'' ÉEA ''P╜»P''' '╕(V╧P)/0' °
X 'L0:╕(0=U╜U-1)/0' '''Incorrect. Try again''' '╕L1' °
XCuc 1 26 ABCDEFGHIJKLMNOPQRSTUVWXYZ °
*(1996 4 6 16 9 33 336) °
Fvec ÉFX 'U╜vec' 'Σ Draw 2d vector in plane' °
'U╜r,r,''2- +'',r '': °'',r,''1- °'',r' °
X 'U╜U,'' : °'',r,''0|....|....|'',r,'' 0 1 2'',r' °