The A Project->Getting Started
Index
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

Getting Started with A+

Using the Console in ASCII Mode

This has to be the first thing everyone will try, so here we go ...

      $mode ascii
      2+2
4

Now you will want to try out subtraction ...

     2-2
 2 -2
     2 - 2
 0

OK, what went wrong the first time? In ASCII mode you must use white space around symbols and names to be safe! A {minus} sign adjacent to a number is a {highbar} in this case. How about some real APL:

     rev 1 2
//[parse] .rev: var?
     reverse 1 2
//[parse] .reverse: var?

     rot 1 2
 2 1
     1 rot iota 5
 1 2 3 4 0

Well, I guessed wrong, probably so did you. The ASCII names are a bit arbitrary and follow the standard Unix convention of 'weird short codes are best' so here is a table to get you started. Probably you will want to print this out and pin it to the monitor (IE5 users can spot the missing domino) ...

APLASCII
APLASCII
APLASCII
APLASCII
:= each ^& ?
* % log ==
max .ff. min in
~= <= >= *^
rot flip iota rho
take drop upg dng
pack unpack bag pick
eval form rtack where
pi mdiv ?rand beam
%ref dot // ~bwnot
^bwand bwor <bwlt bwle
=bweq bwge >bwgt bwne


Note to table: In ASCII mode, every valid outer product .f is replaced by s. (read "s-dot"), where s is the string of symbols that replaces f. For example, >=. replaces ..

We can even define simple functions ...

     total v:{+/v}
     total iota 12
 66
     mean v: {(total v) % #v}

     # 2 3 4
 3
     mean iota 12
 5.5

You use % as {divide} here, but note that A+ implements {tally} with monadic # in J fashion. This makes your function generalise very nicely to higher-rank tables:

     mat := 2 3 rho iota 6
     mat
 0 1 2
 3 4 5
     mat := iota 2 3
     mat
 0 1 2
 3 4 5
     total mat
 3 5 7
     mean mat
 1.5 2.5 3.5
     # mat
 2
     rho mat
 2 3

Hopefully, that is self-explanatory. A+ uses the first dimension by default (like J) and {tally} only sees the outermost element of the shape. You might also notice that {iota} has some built-in reshape capabilities. Dyalog fans might object here ... hard to know which extension is more useful in practice.

What if it all goes wrong?

Sooner or later you will make a mistake, for example mis-typing a name. The A+ equivalent of )reset is $reset (probably you guessed that) and the equivalent of a bare right-arrow is $ on its own, so:

     fred
a[error]  .fred: value
*     $
     2+2
 4

... when faced with * in the prompt string, get into the habit of typing $ at the input prompt to get rid of it.

Multi-line functions are possible - just

This reminds me of typewriter terminals and the {del} editor! Yes, you can create simple functions in the console, but really you should be writing them in a script and running it! Just to show that it can be done:

     fluffy n: {
*     fred := 12;
*     joe := 23;
*     fred + joe + n }

     fluffy 7
 42

Note carefully the semi-colons which act as statement separators. Newlines are completely immaterial, as you can easily show:

     coo n: {
*     fred :=
*     12;
*     fred + n }

     coo
coo n: {
fred :=
12;
fred + n }
     coo 3
 15

Notice that you display a function simply by typing its name. By implication, niladic functions in A+ (although syntactically possible) are messy to use - again just like J and possibly an irritant to APL old-timers? Now watch what happens if you slip up and let your semi-colon reflex run away with you:

     foo x: {x+2;}
     foo 3

Oh, oh. The result of an A+ function is the result of the last executable statement, which in this case is empty. This function ALWAYS returns null. Be careful! Of course it could have a side-effect:

     goo v: { .qq := 12; }
     goo 100
     qq
 12
     hoo v: { my.qq := 25; }
     hoo 0
     my.qq
 25
     qq
 12
     $cx my
     qq
 25

All unqualified names are strictly local, but if you provide a context for the assignment (the root context is just '.' in A+) then the assignment (or reference) becomes global. Note that simply mentioning a context in an assignment is all we need to do to have it created for us. There is a slight oddity here, in that you can create contexts as deep as you like ...

     $cx my.var
     qq := 12
     qq
 12
     $cx .
     my.var.qq
//[parse] my.var: var?

... but the parser only copes with one level of qualification, so this is probably more of a bug than a feature, and we should assume the same capability as J has with locales.

Writing and Running Scripts

Remember that A+ has no concept of a saved workspace. The idea is that you write scripts (in the editor of your choice) and run them in the console. Well, it seems to work for the J community but some of us will soon be hankering after $save, I am sure. Anyway, here is how it is supposed to work. Start the editor and type in something like:


The tool buttons all have tips if you are not familiar with the keyboard layout. Do remember to use the right-hand 'Alt Gr' key to get the APL characters here. Save it with the '.a' extension and switch to the A console to run:

     $load junk
This should show 1 0 1 if we understood correctly!
 1 0 1

Note that this runs in 'APL' mode even though you have set the console running in ASCII. You can set the script to ASCII mode if you prefer to see the APL {highbar} as a conventional minus sign here.

Now you just need to get into the habit of editing your script (probably called test.a or some such), selecting File,Save, clicking over into the A+ window and typing $load test to run it. Remember that you cannot use the up-arrow to recall prior lines here, so keep script names short to save typing! If it fails, $reset, fix it in the editor, save it and try again! Of course scripts can call other scripts, so if you want to have a 'master script' to load lots of others, that will work fine.


Back to TopOn to next section

© British APL Association 2001
Please send any comments to Adrian Smith via adrian@causeway.co.uk - thank you.