Simple Arithmetic (3 of 14)

This is the first practical session. It consists mainly of examples for you to enter and comments on what happens when you do.

You'll be explicitly asked to type in the examples to begin with, but after a few pages it will be assumed that to see an APL statement is to type it, and the explicit instructions will be dropped.

The initial APL display

The exact method of launching your APL application will vary from system to system and vendor to vendor, and you should check the relevant implementation notes for the details.

When you first launch APL you will see a message similar to the following (This example is from APLX - no longer available):

APLX for Windows
Copyright (C) 2001-2008 MicroAPL Ltd
WS Size = 20.0MB, Version = 4.1.6
CLEAR WS

Some APL implementations start in 'calculator' mode which means that each statement you type is executed as soon as you press ENTER. This tutorial generally applies to the calculator mode.

Some arithmetic functions

Type in this statement:

      5+12<enter>
17

Pressing ENTER caused the statement to be executed.

As you can see, what you type is quite easy to distinguish from APL's response. Your input is inset by 6 spaces; in some APLs it will also be in a different colour.

Add is one of the 50 or so functions built-into APL. (A 'function' is simply an operation, in this case, an arithmetic operation). Other familiar arithmetic functions which you'll use in this session are subtract (-) multiply (×) and divide (÷).

Try a couple of divisions:

      18÷3<enter>
6
      108÷11<enter>
9.818181818

Up to 10 digits are typically displayed in a result, though you can change this if you want.

Now type a multiplication:

      4×7<enter>
28

And another with some decimal points:

      3.893×7.6<enter>
29.5868

Subtraction too works as you would expect.

      100-95<enter>
5

The normal minus always means subtract, as in the example you've just typed. The special 'high' minus sign indicates a negative number, as in this example:

      8-16<enter>
¯8

Error Messages

Have you had any error messages yet? You may have if you've made a typing mistake, or if you've tried some of your own examples.

This example will produce an error message:

      23+<enter>
SYNTAX ERROR
      23+
      ^

The text of the message depends on the error. In this case you've broken the syntax rules: 'number function number' makes sense to the APL interpreter. 'number function' does not.

The error marker (^) shows where the interpreter had got to in evaluating the statement when it found the error. This may help you identify the error, but obviously an error may not show up until something incompatible with it is found elsewhere. In such a case the error marker would be pointing to the correct but incompatible part of the statement.

For the time being don't worry too much about error messages. If an error message results from a typing mistake, simply retype the statement correctly. If it results from your own example and you can't see the cause, just try something else.

Most versions of APL include facilities to recall last statement you entered so that you can edit it and enter it again. (Details of how to recall the line vary from one APL vendor to another).

You can try this now. Your screen at the moment should look something like this:

      23+<enter>
SYNTAX ERROR
      23+
      ^

Now recall or re-type the line and complete the statement so that you have:

      23+8<enter>
31

The APL Session

If you are using a GUI version of APL, commands are entered in a Session window. Exact details vary from APL vendor to vendor, so the following is a general guide...

Normally, any text you type is added to the end of the session. If however there is already some text in the window which is exactly what you want to type in, or close to what you want, then there is no need to re-enter the text. You can select existing text on the screen, edit it, and then submit the line to APL.

To do this, you can move the mouse anywhere on the session window and click it. The text cursor (flashing vertical bar) will move to the position at which you clicked. You can now use the normal editing features such as the Delete key or enter further text, and you can also use the Edit menu for more sophisticated editing such as cutting and pasting text. You can continue this process as much as you like - you are actually editing text on the screen only at this stage.

When you press ENTER then the current line - i.e. the line on which the cursor is flashing - will be re-displayed at the end of the session window, and submitted to APL.

Arithmetic on lists of numbers

Now we get on to something more interesting. Try this statement, making sure you include the spaces between the numbers:

      3+2 4 11 7 5
5 7 14 10 8

The number on the left of the sign has been added in turn to each number on the right of the sign.

Obviously, with lists of numbers, spaces do count. There's a great deal of difference between:

      1+2 3 4

and

      1+234

as you'll have seen when you typed them both in.

The list can be on either side of the sign. Here it's on the left and the single number's on the right:

      6 3 8 1+3
9 6 11 4

Now try some of the other arithmetic operations on lists. Here the divide function is used to divide each number in a list by 15:

      2.5 33.7 12 8÷15
0.1666666667 2.246666667 0.8 0.5333333333

And here's an example using the multiply function:

      9.8 11.2 17 1.2×1.175
11.515 13.16 19.975 1.41

In the last example you could be doing something useful such as multiplying a list of prices by 1.175 to give the prices including VAT (Value Added Tax) at 17.5%.

Matching up list lengths

So far the examples have involved a single number on one side of the arithmetic sign and a list of numbers on the other. But you can do arithmetic on two lists of numbers:

      12 3 29 4×1 3 5 2
12 9 145 8

The first number in the list on the left was multiplied by the first number in the list on the right, the second by the second and so on. But notice that the list on the left contained the same number of items as the list on the right.

Try this example to find out what happens if the lists don't conform in size:

      3 5+4 1 5

As you see, you get an error message like this:

LENGTH ERROR
      3 5+4 1 5
      ^

Since there are two numbers in one list and three in the other, the system doesn't know which number to add to which.

If a function operates on two lists they must both have the same number of elements. It's quite in order, though, to do an operation involving a single number and a list.

Order of execution

So far there's been no doubt about the order in which parts of a statement are executed because the examples haven't contained more than one arithmetic function. Now here's a statement which contains both multiply and subtract. Type it in, but decide what the answer will be before you press ENTER:

      3x3-1

Possibly you think the multiplication will be done first (either because it appears first in the line, or because you expect multiplication to be done before addition or subtraction). In that case you think the answer is 8.

Press ENTER and see.

      3×3-1
6

In fact APL always works from right to left. So it first did 3-1 giving 2. Then it did the multiplication. (There's no hierarchy of operations in APL because there are too many functions in the language for one to be practicable - imagine trying to remember the order of precedence of fifty different operations!)

Here's another example of execution order. Again see if you can predict the answer before pressing ENTER:

      2 3 1+8÷2 2 2

The system first evaluates 8÷2 2 2 giving 4 4 4. Then it does 2 3 1+4 4 4 giving 6 7 5. Press ENTER and confirm this:

      2 3 1+8÷2 2 2
6 7 5

Parentheses

If you want to force a particular execution order, you can do so by using parentheses. Please retype the last example, using parentheses to force execution of the addition first:

      (2 3 1+8)÷2 2 2
5 5.5 4.5

Negative numbers and subtract

You saw earlier that the minus sign means 'subtract' and the high minus sign indicates a negative number. Here subtract is used:

      1985 - 1066
919

Here is an example with negative numbers in two lists which are being added together:

      3 ¯1 ¯7 + ¯4 ¯1 2
¯1 ¯2 ¯5

The minus-sign used to indicate negative numbers is known as the 'high-minus', and helps to make clear the difference between a negative number and the subtraction operation. The 'high-minus' is usually found above the number '2' on the keyboard.

These two examples illustrate this:

      2-3+5
¯6
      2 ¯3+5
7 2

In the first example, the sum of 3 and 5 was subtracted from 2. In the second example, ¯3 was interpreted as a negative number. So 2 ¯3 was treated as a two-element list and each element was added to 5.

Next we're going to consider a feature which effectively doubles the number of functions at your disposal. Then we're going to round things off with two new functions.

Dual-purpose functions

The examples you've seen so far have taken the form:

For example:

Each number, or list of numbers, operated on by a function is said to be an argument, so all our examples have shown a function operating on two arguments.

You may disagree, thinking perhaps of examples like this: `3x3+1}}}

In fact each function in that example does have two arguments. The subtract function has the arguments 3 and 1. The Multiply function has 3 and the result of 3+1.

There is, however, an alternative to having an argument on either side of the function. You can use any of the arithmetic functions in this format instead:

For example:

But when used in this way the functions have a different effect. Before reading any further please experiment with + - × ÷, putting a number, or list of numbers, on the right-hand side only. See if you can decide what (if anything!) each function appears to do.

*********

What did you conclude? You probably weren't unduly impressed by + since it appears to do nothing. We'll find a use for it though in the next chapter. You'll have discovered that minus effectively reversed the sign of everything to its right:

      - 3 ¯6 ¯8 4 12 ¯9
 ¯3 6 8 ¯4 ¯12 9

The action of ÷ may have puzzled you:

÷1 2 4 10 100
1 0.5 0.25 0.1 0.01

In fact ÷ used in this way yields the reciprocal of each number on its right (i.e. the result of dividing 1 by that number).

To put it another way, you can think of ÷3 6 2 as being equivalent to 1÷3 6 2.

You may have concluded that × simply produced lists of 1s:

      x2 66 8 2 13 109
1 1 1 1 1 1

This example gives a clue to its real purpose:

      x8 0 ¯3 ¯7 0 4
1 0 ¯1 ¯1 0 1

It returns a value of 1 for each positive number, ¯1 for each negative number and 0 for 0s.

Now it doesn't really matter whether you remember what each of these signs does when used in this 'one-sided' format - you can always look it up if the need arises. What does matter is that you appreciate that many APL functions are capable of two interpretations. This flexibility effectively doubles APL's repertoire.

A small point before we move on.

When you typed the following expression earlier in this chapter you got an error message:

      23+
SYNTAX ERROR
      23+
      ^

This is because though the pattern 'function number(s)' is recognised by the interpreter, 'number(s) function' is not.

Ceiling and Floor

To round things off we'll look at two functions that find maxima and minima. These are (usually called Ceiling or Max) and (usually called Floor or Min), and they are usually found above the 'S' and 'D' keys respectively.

We've had one or two examples like this:

      145÷11
13.18181818

Examples, that is, that produced a lot of decimal places. Now the eight places following the point in this example may be essential, but if they aren't, it's easy to get rid of them.

The function rounds up to the nearest whole number.

The function rounds down to the nearest whole number.

      ⌈13.18181818
14
      ⌊13.18181818
13

They both work on lists as well as on single numbers:

      ⌈120.11 12.32 65.01 13.52
121 13 66 14
      ⌊99.99 12.82 15.39 48.90
99 12 15 48

Since these functions simply round up or down to the nearest whole number, in the first example 120.11 is rounded up to 121 and, in the second, 99.99 is rounded down to 99.

If you want greater accuracy in rounding, a simple piece of arithmetic will achieve it:

      ⌈120.11 12.32 65.01 13.52 - 0.5
120 12 65 14

Now 0.5 is subtracted from each number in the list before rounding up is carried out, thus ensuring a truer result. Rounding down can be corrected in a similar way by adding 0.5 to the numbers to be rounded:

      ⌊99.99 12.82 15.39 48.90 + 0.5
100 13 15 49

Possibly you've noticed that all the examples for and have taken the one-sided form:

Used with two arguments, these functions have a different meaning. Try out some statements of the form:

and decide what the two-argument versions of and do.

******

As you no doubt discovered, selects the bigger of two numbers:

      2 ⌈ 6
6

while selects the smaller:

      2 ⌊ 6
2

If is used on lists of numbers, the first number in list 1 is compared with the first number in list 2, the second with the second and so on. The 'winner' in each comparison is displayed as the result:

      6 8 1 ⌈ 3 5 9
6 8 9

The equivalent procedure occurs with :

      6 8 1 ⌊ 3 5 9
3 5 1

Summary

We won't summarise everything: you can refer to your APL's manual for definitions of the functions we've covered so far (+ - × ÷ ).

There are, however, three points made in this chapter that are essential to your understanding of APL:

1) APL always works from right to left, passing intermediate results along the line to the left as it works them out.

2) A single number and a list can be involved in an operation, but if two lists are involved, they must be the same size (i.e. have the same number of elements).

3) Many functions can be used with either one or two arguments. The number of arguments determines what the function does.

Practice

See what you can do with the functions covered so far. If you run out of ideas, why not look up '/' in an APL manual? It greatly increases the scope of the arithmetic (and other) functions. It will, however, be covered in a later session so don't feel you have to master it now. When you've tried things out to your satisfaction, do the problems below.

When you want to end the session, type:

      )OFF

Problems

Q1. Enter statements to:

a) Multiply each of the three numbers, 3 6 2 by 8 and then add 4 to the results of the multiplication.

b) Add 15% to each number in the list 14 5 78 145.

c) Add the difference between 13 and 8 to 4 6 12 7.

d) Multiply the result of 6 times 3 by the result of 4 times 8 and subtract 5 from the total.

e) Reverse the signs in this list: 3 ¯4 ¯12 6

f) Compare these lists, selecting the larger number in each comparison:

Q2. Which of these statements cause error messages? Why?

a) 12×9

b) 3+¯2

c) 19 0 3 4÷7 2 87

d) 5 ¯8

Q3. You're getting £200 worth of Dollars for yourself and £180 and £230 worth respectively for two friends. Enter a statement which calculates how many dollars each of you will get at 1.96 dollars to the pound.

Q4. Highest recorded temperatures for a week in August were:

Enter a statement to convert them into Centigrade. (One method is to subtract 32 degrees and multiply by 5/9.) Suppress decimal places in the result.

Q5. Enter a statement to find the difference in metres between 1500 metres and a mile. (1 yard = 0.9144m and 1760 yards in a mile)

Answers

(Your results should be the same. Your APL code may be different.)

Q1 a)

      4+3 6 2 × 8
28 52 20

b)

      14 5 78 145×1.15
16.1 5.75 89.7 166.75

c)

      4 6 12 7+13-8
9 11 17 12

d)

      ¯5+(6×3)×4×8
571

e)

      - 3 ¯4 ¯12 6
¯3 4 12 ¯6

f)

      2 7 0 55 ⌈ 33 1 10 13
33 7 10 55

Q2. The only one to cause an error message is:

c)

      19 0 3 4÷7 2 87
LENGTH ERROR
      19 0 3 4÷7 2 87
      ^

(The lists aren't the same size.)

Though it doesn't produce an error, statement d) isn't achieving much:

d)

      5 ¯8
5 ¯8

APL thinks it's a two-element list. If the intention was to subtract the value 8 from 5. the following would have been more effective:

      5-8
¯3

Q3.

      200 180 230 x 1.96
392 352.8 450.8

Q4.

      ⌈ ¯0.5+(5÷9)×79 84 83 78 74 69 70-32
26 29 28 26 23 21 21

(Work through it from right to left if it doesn't immediately make sense to you.)

Q5.

      1500-1760×.9144
¯109.344

(1500 metres is shorter than 1 mile)


CategoryAboutApl

LearnApl/SimpleArithmetic (last edited 2017-02-16 19:18:01 by KaiJaeger)