As in the last practical session, please type in the examples as you encounter them. If you don't, you'll find parts of the session difficult to follow.
This is an assignment:
A ← .175
A location or 'variable' called
200 × A 35 A × 30.50 12,25 60.30 15.00 5.3375 2.14375 10.5525 2.625 ⌈ A × 30.50 12.25 60.30 15.00 6 3 11 3
Variables are convenient if you want to use the same value in a number of operations, specially if it's a value that's a nuisance to key in, like this one:
C ← .45359237
17 × C 7.71107029
Or to calculate how many kilos there are in 11 stones and round up the answer:
Variables are also useful if you want to keep results. The result of the calculation you've just done may still be on the screen, but it isn't preserved in the computer's memory. If you had wanted to keep it you could have typed:
JOE ← ⌈C×11×14
The result is now saved in
That last example showed that names needn't be a single letter. See if you can deduce some of the other rules about variable names by typing in the following statements and seeing which ones produce error messages or unexpected results:
AAA ← 4 ab ← 1 5B ← 12 C9999 ← 0 JOHN SMITH ← 100 JILL∆SMITH ← 100 Jack_Smith ← 100
One error was:
JOHN SMITH ← 100 VALUE ERROR JOHN SMITH ← 100 ^
That particular error occurred because of the space between
5B ← 12 5 12
Names can't contain spaces or any other symbol except the Delta (
It's possible to produce duplicate variable names in the same workspace under special circumstances, as you'll find out when you write a user-defined function. But in calculator mode, names have to be unique; if you assign a value to
Assigning lists to variables
So far we've assigned single numbers to variables. This example puts a list of numbers into a variable called
PRICE ← 12.45 5.60 5.99 7.75
And this statement multiplies the numbers in
VAT ← PRICE × A
To see the contents of VAT type:
VAT 2.17875 0.98 1.04825 1.35625
Incidentally, in the last chapter we mentioned that the one-argument version of
+VAT ← PRICE×A 2.17875 0.98 1.04825 1.35625
You've seen two ways of setting up a variable. One is to directly assign a value to it; the variable
What you can't do is use a variable name that hasn't had a value assigned to it. See what happens if you type:
A-BB VALUE ERROR A-BB ^
No value has been assigned to variable
Loosely speaking, system commands manage the environment in which you work rather than doing calculations or manipulating data. You've already met one system command (don't type it unless you want to end the session!):
The right parenthesis identifies system commands. If you use
Here's a system command which lists the names of the variables you've got in your workspace:
)VARS A C JOE PRICE VAT
Your list may be different if you've set up all the examples and some other variables.
Now try this one;
)WSID CLEAR WS
You've asked the identity of the workspace and the system has told you it's called
The same command can be used to change the identity of the workspace. Here you're changing its name to
)WSID NEW WAS CLEAR WS
The system seems more concerned about the old name, but it has in fact carried out your request as you'll find if you type:
Though the name has changed, the variables are still there as you'll see if you again type:
VARS A C JOE PRICE VAT
We don't need to keep these variables, so we'll use another system command to clear the workspace:
)CLEAR CLEAR WS
The variables are no longer there, as you can confirm by typing:
And the name of the workspace has reverted to
)WSID CLEAR WS
We'll cover other system commands in later sessions. For now we'll return to assignments.
You could be forgiven for thinking that APL deals only in numbers. But it isn't so. Try this assignment, taking care to put the single quote mark round the piece of text:
A ← 'APL WILL PROCESS TEXT'
Now check what's in
A APL WILL PROCESS TEXT
The characters between the quotes have obviously been put into
C ← CHARACTERS VALUE ERROR C ← CHARACTERS ^
This time include the quotes, then check that the assignment has worked:
C ← 'CHARACTERS' C CHARACTERS
In fact, APLX caters for the forgetful as far as quotes are concerned. If you put an opening quote, type your text, then press ENTER without putting the closing quote, APL will insert it for you:
MORAL ← 'WELL BEGUN IS HALF DONE MORAL WELL BEGUN IS HALF DONE
This prevents succeeding lines being treated as one very long text string, simply because you've forgotten to close a quote. You can use any characters on the keyboard in a character assignment. That includes space (which is a character, albeit an invisible one) and all the symbols. You can even include the single quote character itself, though that requires some special action.
Try this assignment:
NAME ← 'WHAT'S IN A NAME? '
As you probably expected, you got an error message. APL can't distinguish between the apostrophe in the text and the single quote marks round the text. If you need to include an apostrophe (i.e. a single quote) insert another single quote beside it like this:
NAME ← 'WHAT''S IN A NAME? ' NAME WHAT'S IN A NAME?
Alternatively, you can use double quote marks like this:
NAME ← "WHAT'S IN A NAME? " NAME WHAT'S IN A NAME?
APLX allows you to use single or double quote marks round the text. The opening and closing quote marks must be of the same type; a quote mark of the other type doesn't end the text, as the example above shows.
Now let's set up a variable containing the characters,
N ← 'NET PRICE'
Obviously a variable containing characters can't be used in arithmetic, but try this all the same:
Well, as you saw you got a new kind of error:
N×10 DOMAIN ERROR N×10 ^
The domain of characters and the domain of numbers have different rules and their members don't mix. This applies even when characters look like numbers. Type this assignment, taking care to include the quotes:
QTY ← '230'
The fact that
QTY+5 DOMAIN ERROR QTY+5 ^
If you are getting a bit tired of making all these individual assignment statements, APLX allows you to set up several variables at the same time. Try typing:
(ZAK YAK) ← 5
then see what values have been assigned to
(YEN MARK BUCK) ← 10 20 30
There you are, three at a single blow!
Displaying variables together
Though you can't use character and number variables together in arithmetic, they are prepared to appear together in displays.
N 10 NET PRICE 10
(If your version doesn't contain the same space between
Here two character variables are displayed in tandem:
NAME C WHAT'S IN A NAME? CHARACTERS
And this example shows two numeric variables displayed together (but first you have to set them up):
X ← 18 Y ← 3 1985 X Y 18 3 1985
In this example we're mixing domains again:
NAME X C WHAT'S IN A NAME? 18 CHARACTERS
That statement is true: the variable,
C C C CHARACTERS CHARACTERS CHARACTERS
You can use characters in APL without putting them in a variable first. For example:
'NET PRICE: ' 10 NET PRICE: 10
Joining lists together
The way we've been displaying lists together is more powerful than you may think. We've treated it up till now as a means of displaying the contents of lists together. In fact it actually joins lists so that two single numbers form a two-element list, or two character strings form one two-element character object.
So when you entered the statement
1+X Y 19 4 1986
If you wanted to use the list formed by
Z ← X Y Z 18 3 1985
This has the advantage that
Z ← Z+10 Z 28 13 1995 X 18 Y 3 1985
Here's an example with characters for a change:
CNAME ← 'BASIL ' SNAME ← 'BRUSH' NAME ← CNAME SNAME NAME BASIL BRUSH
This example is actually rather complicated, as the contents of
Joining and merging variables
If we want to join the characters in the last example to form a single list, we have to use a new symbol
NAME ← CNAME,SNAME NAME BASIL BRUSH
Simple and nested variables
So how does APL work out which variables or lists are lists of lists (which is another way of describing our nested variable) or just simple lists?
There are some easy rules to follow when typing in data.
Single numbers and characters are interpreted as making up simple lists. So here is a list of 4 numbers:
PIERRE ← 1 2 3 4
and here is another list of five characters:
MIREILLE ← 'FILLE'
If some of the numbers are enclosed by parentheses they are treated as a single item in the resulting list:
PIERRE ← (1 2 3) (4 5 6 7)
FRANCOISE ← 'UNE' 'JEUNE' 'FILLE'
Just to complete the story, APLX allows a variable to have both character data and number data in it. You won't be able to carry out arithmetic on the character part, but this is a useful way of storing characters and numbers that 'belong' together:
PHONES ← 'BILL' 577332 'FRANK' 886331
Variable names are formed from any combination of the letters of the alphabet (in uppercase or lowercase) and the numerals 0 to 9, together with the characters
Any numeric value can be assigned to a variable. This includes a single number, a list of numbers or the result of a calculation.
Any character on the keyboard can be assigned to a variable.
Character strings assigned to a variable are enclosed in single quotes. To include a single quote in a character string, type another single quote immediately beside it, or use double quotes.
Character variables can't be used in arithmetic.
More than one variable can be assigned at the same time.
The contents of variables can be displayed together merely by typing their names together.
Variables can contain a mixture of numbers and characters.
Variables are made up of single numbers or characters, in which case they are called simple variables, or their elements can be lists themselves, in which case they are called nested variables.
The comma performs the catenate function. Variables joined by it can be treated as a single variable.
System commands manage the environment provided by the system. The following were mentioned in this chapter:
)VARS )WSID )CLEAR )OFF
Please experiment with setting up and displaying character variables for a few minutes before you do the problems. Clear the workspace when you've finished,
Q1. Enter statements which:
a) Assign the numbers
b) Assign the characters
c) Produce the display:
Q2. Set up a variable
Q3. The cost prices of four items of stock are
Q5. Which of the following will produce error messages? Why?
D ← 22 M ← 2 y ← 2007
(D M Y) ← 22 2 2007
DATE ← 'TODAY''S DATE:'
DATE ← "TODAY'S DATE:"
DATE D M Y TODAY'S DATE: 22 2 2007
CONV ← .454 ⌊ .9×CONV×13×14 74
(the weight used in this calculation was 13 stone,)
LIST1 ← 2×8 6 12 4 LIST2 ← 1.75×16 13 7 VATPRICE ← ⌈ 1.175× LIST1,LIST2 'PRICE+VAT: ' VATPRICE PRICE+VAT: 19 15 29 10 33 27 15
TEST1 ← 65 72 54 80 67 60 59 TEST2 ← 75 70 60 74 59 61 50 TEST1 ⌈ TEST2 75 72 60 80 67 61 59
b) produces a
c) produces a
e) produces a
Copyright © 1996-2008 MicroAPL Ltd