Home First Encounter Current Status GNU License Downloads Getting Started Examples Mississippi Overview of A Structure of Data Syntax Relation to other APLs Language Reference System Fns and Vars Functions Where next Quibbles Materials Vector Home |
Some Examples from “APL for the Mathematics Classroom”One way to get a quick feel for the compatibility of A+ and APL is to translate a few useful code fragments from one to the other. Norman Thomson's book is a good starting point, as he wrote this with I-APL in mind, so it is more or less in 'Direct Definition' style already. Here are a few functions from the section on Random Numbers from Various Distributions (pages 106-107). Random NumbersCompare this with Norman's originals ... ã R random numbers from the uniform distribution in the range (0,1) RND R: {(?T)ßTûRÒ100000} ã L random numbers from uniform distribution in the interval R[1],R[2] L RUN R: {R[0]+(RND L)«R[1]-R[0]} ã L random numbers from -ve exponential distribution, mean R L RNE R: {-R«ðRND L} ã L random numbers from Normal dist, mean R[0] and SD R[1] L RNO R: {R[0]+R[1]«((2«L RNE 1)*0.5)«2ÏÏ2«RND L} ã L random booleans where probability of drawing 1 is R L RBO R: {R>RND L} We need to switch to zero-origin indexing, and we also discover that Norman's use of -/R[2 1] must be rewritten in a more mundane way. The rest of it looks (and works) just as it did before: $load maths RND 5 0.24219 0.10639 0.81261 0.4204 0.89144 4 RUN 10 20 13.1941 10.7872 13.5569 11.4972 20 RBO .5 0 0 1 0 0 0 1 0 1 1 1 0 1 0 0 1 0 1 0 0 A few more functions from the following page ... ã L random vars from the set ÉÒR ... L RSA R : {+/@1(RND L)Ê.¦0,¢1Õ+\R} ã Frequency distribution for a sample such as the above L RFD R : {+/@1(1+ÉÒR)Ê.=ÓL RSA R} Note that we need to specify the axis on the sum (as A+ defaults to the first axis) and adjust for the index origin in the second example. Newton-Raphson MethodThis is more challenging, as it requires unpicking the 'old-style' compare-and-branch style of switching to the modern approach with control structures. Here are the two versions again: L NEWRAP R: { ã Root-finder by NR method Tû(RÉ';')ÙR; T1û(1+RÉ';')ÕR; ã Split expr;derivative XûL[2]; Iû0; while (L[0]<|X-Z Ý ZûX-(âT)ß(âT1)) { IûI+1; if (0=L[1]|I) ã See some output Õ'AT ITERATION',(îI),' X=',(3ÕL)îX; XûZ }; 'SOLUTION = ',((3ÕL)îX),' AT ITERATION',îI } ã Example from page 94 Testnr R : {R NEWRAP '(X*2)-2;2«X'} Again the index-origin change hits us - the code to split the string is a less-obvious amendment here. Note the use of the {downarrow} to get A+ to spit out the intermediate results to the console. Also the syntax for dyadic format is slightly different which changes the way we run the example. The positive thing about this example is that the overall structure and layout will look very familiar to anyone who has written JavaScript or PERL; with a few renamed variables it should be very acceptable to the 'modern' programmer! Testnr .00001 2 2 7.4 AT ITERATION 2 X= 1.5000 SOLUTION = 1.4142 AT ITERATION 3 Over to you!Maybe someone would like to have a go at something 'substantial' in A, for example the VierNeun puzzle or the 'funny cube' would be good subjects where there is already code in APL, J and K for comparison. Please send in any examples to the address at the foot of the page and we will include as many as we can in this section of the site.
© British APL Association 2001 Please send any comments to Adrian Smith via adrian@causeway.co.uk - thank you. |