The A Project->Getting Started->Examples
  First Encounter
  Current Status
  GNU License
Getting Started
Overview of A
  Structure of Data
  Relation to other APLs
  Language Reference
  System Fns and Vars
Where next

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 Numbers

Compare this with Norman's originals ...

 R random numbers from the uniform distribution in the range (0,1)
 RND R: {(?T)TR100000}

 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 random numbers from Normal dist, mean R[0] and SD R[1]
 L RNO R: {R[0]+R[1]((2L RNE 1)*0.5)22RND L}

 L random booleans where probability of drawing 1 is R

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 Method

This 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
 XL[2]; I0;

 while (L[0]<|X-Z  ZX-(T)(T1)) {
  if (0=L[1]|I)   See some output
   'AT ITERATION',(I),' X=',(3L)X;
  XZ };


 Example from page 94
 Testnr R : {R NEWRAP '(X*2)-2;2X'}

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

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.

Back to TopOn to next section

© British APL Association 2001
Please send any comments to Adrian Smith via - thank you.