![]() |
![]() |
|
Simple Arithmetic |
|
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. To download a time-limited evaluation copy of APLX so you can try this, visit our Download page The initial APLX displayThe exact method of launching the APLX application will vary from system to system, and you should check the relevant implementation notes for the details. When you enter APLX you will see the first APL display, the one which includes the copyright statement, the version number, the workspace size and the message that the workspace currently in memory is called 'CLEAR WS'. The message should look like: APLX for Windows Copyright (C) 2001-2008 MicroAPL Ltd WS Size = 20.0MB, Version = 4.1.6 CLEAR WS (Exact details about the version number, WS size, etc, may vary) APL starts in calculator mode which means that each statement you type is executed as soon as you press ENTER. Some arithmetic functionsType 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 and if you are using a GUI version of APLX (Windows, Macintosh or Linux) 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 ( Try a couple of divisions: 18÷3 <enter> 6 108÷11 <enter> 9.818181818 Up to 10 digits are normally 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 MessagesHave 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 ( There's a list of error messages and their likely causes in the APLX Language Manual. But for the time being don't worry too much about them. 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. If you want to edit the last statement (rather than retype it) simply press Ctrl and Up-Arrow (or Command and Up-Arrow on the Macintosh, or on some systems Ctrl and R, or the 'last line recall' key) and the statement will appear again with the cursor at its end. You can try this now. Your screen at the moment should look like this: 23+ <enter> SYNTAX ERROR 23+ ^ Now recall the line (Ctrl and Up-Arrow, or Command and Up-Arrow on the Macintosh) and complete the statement so that you have: 23+8 <enter> 31 Using the Session WindowIf you are using a GUI version of APLX (Windows, Macintosh or Linux), APL commands are entered in the Session window: 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 of APLX 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. The line you changed will be returned to its former state. Arithmetic on lists of numbersNow 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 lengthsSo 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 executionSo 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 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 2 3 1+8÷2 2 2 6 7 5 ParenthesesIf 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 subtractYou 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 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 functionsThe examples you've seen so far have taken the form: number(s) function number(s) 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: In fact each function in that example does have two arguments. The subtract function has the arguments 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: function number(s) For example: But when used in this way the functions have a different effect. Before reading any further please experiment with ********* What did you conclude? You probably weren't unduly impressed by - 3 ¯6 ¯8 4 12 ¯9 ¯3 6 8 ¯4 ¯12 9 The action of ÷1 2 4 10 100 1 0.5 0.25 0.1 0.01 In fact To put it another way, you can think of You may have concluded that 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 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 at the APLX Language Manual 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 (
|
<< Previous | Contents | Next >> |
Home APLX Overview Buy APLX |
Copyright © 1996-2008 MicroAPL Ltd