#acl AutoAdminGroup:read,write,admin,delete,revert All:read {{{#!html
}}} ||<[[../SimpleArithmetic|Back]] || [[../TutorialContents|Contents]] || [[../AplTables|Next]]>|| {{{#!html
}}} = Variables (4 of 14) = <> 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. == Assignments == This is an assignment: {{{ A ← .175 }}} A location or 'variable' called {{{A}}} is set up in memory and the value {{{0.175}}} is assigned to it. An assignment doesn't automatically cause the value to be displayed, but it's there. If you want it displayed, just type the variable's name: {{{ A 0.175 }}} Now {{{A}}} can be used in any expression in which {{{0.175}}} could be used. Here are three examples: {{{ 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 }}} The variable {{{C}}} now contains a conversion factor for converting pounds to kilograms. (1 lb = 0.45359237 kg). You can use {{{C}}} whenever you want to do such a conversion; for example, to find out how many kilos there are in 17 lbs: {{{ 17 × C 7.71107029 }}} Or to calculate how many kilos there are in 11 stones and round up the answer: {{{ ⌈C×11×14 70 }}} 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 {{{JOE}}}. To see it type: {{{ JOE 70 }}} == Variable names == 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 {{{JOHN}}} and {{{SMITH}}}. APL assigned the value {{{100}}} to {{{SMITH}}} (type it again and type {{{SMITH}}} to see) but got stumped by the name {{{JOHN}}}, for which there is no value set at the moment. Watch out: this is not an error in Dyalog. In Dyalog, the above expression assigns the scalar 100 to each of the listed names. Other interpreters which support strand notation would also allow this if you used parentheses: {{{(JOHN SMITH) ← 100}}} Another error was: {{{ 5B ← 12 5 12 }}} This time, {{{B}}} was given the value {{{12}}} and then the value of {{{B}}} was displayed after the number {{{5}}}. This point is covered in more detail later in this section The exact rules on variable names vary slightly from one APL vendor to another. In general the letters A-Z are OK; and numerals are OK, though not as the first character. Many APLs also allow the symbols Delta ({{{∆}}}), underlined Delta ({{{⍙}}}), the underline ({{{_}}}), and the high minus ({{{¯}}}). 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 {{{A}}} then decide to set up another variable {{{A}}} containing something else, all you'll do is give the original {{{A}}} a new value. == 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}}}: {{{ PRICE ← 12.45 5.60 5.99 7.75 }}} And this statement multiplies the numbers in {{{PRICE}}} by the variable {{{A}}} (to which you earlier assigned the value {{{0.175}}}) and puts the results in {{{VAT}}}. {{{ 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 {{{+}}} did have a use. It's a declarative, that is, it causes a value to be declared (i.e. displayed). In the previous example, if you'd wanted to see the contents of {{{VAT}}} without having to type its name, you could have put a plus in front of it: {{{ +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 {{{A}}} was set up in this way by the statement {{{A ← O.175}}}. The other is to assign a result to it: the variable {{{VAT}}} was set up in this way by the statement {{{VAT ← PRICE × A}}}. 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 {{{BB}}} (unless you've used {{{BB}}} in your own examples). APL therefore declares a {{{VALUE ERROR}}}. How can you check on what variables do exist in your workspace? This is a convenient point at which to introduce a couple of system commands. == System Commands == Loosely speaking, system commands manage the environment in which you work rather than doing calculations or manipulating data. The exact list of system commands will differ from one APL to another, but there is a common subset which most interpreters support. You've already met one system command (don't type it unless you want to end the session!): {{{ )OFF }}} The right parenthesis identifies system commands. If you use {{{OFF}}} or any other system command without this parenthesis, it won't work: the interpreter thinks it's a variable name and (probably) produces an error message. 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 {{{CLEAR WS}}}. The same command can be used to change the identity of the workspace. Here you're changing its name to {{{NEW}}}: {{{ )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: {{{ )WSID NEW }}} 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: {{{ )VARS }}} And the name of the workspace has reverted to {{{CLEAR WS}}}, as you'll find if you again type: {{{ )WSID CLEAR WS }}} We'll cover other system commands in later sessions. For now we'll return to assignments. == Character 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}}}: {{{ A APL WILL PROCESS TEXT }}} The characters between the quotes have obviously been put into {{{A}}}. If you omit the quotes, APL thinks it's dealing with an undeclared variable: {{{ C ← CHARACTERS VALUE ERROR C ← CHARACTERS ^ }}} This time include the quotes, then check that the assignment has worked: {{{ C ← 'CHARACTERS' C CHARACTERS }}} 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, some versions of APL allow you to use double quote marks like this: {{{ NAME ← "WHAT'S IN A NAME? " NAME WHAT'S IN A NAME? }}} 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, {{{NET PRICE}}}: {{{ N ← 'NET PRICE' }}} Obviously a variable containing characters can't be used in arithmetic, but try this all the same: {{{ N×10 }}} 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 {{{230}}} is in quotes ensures it is treated as three characters with no more numerical significance than {{{ABC}}}. You can prove this by trying to use it numerically: {{{ QTY+5 DOMAIN ERROR QTY+5 ^ }}} == Multiple assignments == If you are getting a bit tired of making all these individual assignment statements, APL allows you to set up several variables at the same time. Try typing: {{{ (ZAK YAK) ← 5 }}} then see what values have been assigned to {{{ZAK}}} and {{{YAK}}}. They both should have the value {{{5}}}. Each separate name inside the parentheses has been given the value {{{5}}}. If you want to set up some other variables with different values, try typing: {{{ (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 {{{PRICE}}} and {{{10}}}, it's because you may have left more or less space between {{{PRICE}}} and the closing quote when you made the assignment to {{{N}}}.) 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, {{{NAME}}}, does contain 18 characters, counting all spaces (including the one at the end). As you probably realise, any spaces you want have to be included between the quotes. Inserting them between the variable names like this achieves nothing: {{{ 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 {{{X Y}}} to produce the display {{{18 3 1985}}}, you in fact produced a two-element list which could be used in arithmetic. You can prove this now by typing: {{{ 1+X Y 19 4 1986 }}} If you wanted to use the list formed by {{{X}}} and {{{Y}}} more than once, you could assign it to a variable: {{{ Z ← X Y Z 18 3 1985 }}} This has the advantage that {{{X}}} and {{{Y}}} exist independently as well as being components of the list which forms {{{Z}}}. So operations done to {{{Z}}} don't affect them. The following example adds {{{10}}} to {{{Z}}} and then displays {{{Z X}}} and {{{Y}}} to show that only {{{Z}}} has been affected: {{{ 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 {{{NAME}}} are actually only '''two''' elements. The first element is a list which contains the characters {{{BASIL}}} and the second is a list which contains the character list {{{BRUSH}}}. This is an example of a special type of variable known as a '''nested''' variable - we'll be discussing this later in more detail when we've covered more of the basics ({{{Z}}} was nested too). If you want a preview of a later section, try using {{{⍴}}} (called '''rho''') in front of some of these variables and see what it does. == 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 {{{,}}} (comma). The comma tells APL to merge the data on its left and right. Let's try the last example again: {{{ NAME ← CNAME,SNAME NAME BASIL BRUSH }}} This time {{{NAME}}} is a list of 11 characters. In the next chapter we'll be looking at how to use some new symbols to tell the difference between this simple variable and its nested variant. The comma is in fact a function in its own right, and the function is performs is called '''catenation'''. == 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) }}} So here {{{PIERRE}}} is a list of two lists, one of which is three long and one of which is four long. To make a list of character lists, all you need to do is to use groups of characters, each of which are enclosed by quotes. So to make up a list of three character lists: {{{ FRANCOISE ← 'UNE' 'JEUNE' 'FILLE' }}} == Mixed variables == Just to complete the story, APL 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 }}} {{{PHONES}}} will be four elements long, and will be alternately character lists and single numbers. == Summary == The symbol {{{←}}} assigns numbers or characters to a named variable. The right argument is the value to be assigned. The left argument is the name of the variable. Variable names are formed from any combination of the letters of the alphabet (in uppercase or lowercase) and the numerals 0 to 9. Versions of APL from most vendors will allow some other characters including {{{∆}}} {{{⍙}}} {{{_}}} and {{{¯}}}, but you should check your APL interpreter's documentation. 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 if the APL supports it. 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 }}} == Practice == 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, == Problems == '''Q1.''' Enter statements which: a) Assign the numbers {{{22 2 2007}}} to three variables called respectively {{{D M}}} and {{{Y}}}. b) Assign the characters {{{TODAY'S DATE:}}} to a variable called {{{DATE}}}. c) Produce the display: {{{TODAY'S DATE: 22 2 2007}}} (Use the correct date if you prefer.) '''Q2.''' Set up a variable {{{CONV}}} which contains a constant for converting pounds to kilos. (1 lb = 0.454 kg and 14 lb = 1 stone) Use {{{CONV}}} to convert your weight (to the nearest stone) into kilograms. Reduce the result by 10%, round it down, and display it. '''Q3.''' The cost prices of four items of stock are {{{£}}}8 6 12 4 respectively. The markup on these items is 100%. Three other items cost respectively {{{£}}}16 13 and 7. Their markup is 75%. Calculate the fully inclusive price of each item (with VAT at 17.5%). Display the prices (rounded up) with the caption: {{{ 'PRICE+VAT: ' }}} '''Q4.''' {{{TEST1}}} contains a student's exam marks for each of seven subjects (65 72 54 80 67 60 59). {{{TEST2}}} contains his marks for the same subjects gained at a different test (75 70 60 74 58 61 50). Produce a list consisting of his higher mark for each subject. '''Q5.''' Which of the following will produce error messages? Why? a) {{{RATE ← '3.7x3' }}} b) {{{10+10 '←21'}}} c) {{{100×RATE}}} d) {{{SYMBOLS ← '¯<≤=≥'}}} e) {{{3+'232'}}} '''Answers''' '''Q1.''' a) {{{ D ← 22 M ← 2 y ← 2007 }}} or {{{ (D M Y) ← 22 2 2007 }}} b) {{{ DATE ← 'TODAY''S DATE:' }}} or, if your APL supports double quotes, {{{ DATE ← "TODAY'S DATE:" }}} c) {{{ DATE D M Y TODAY'S DATE: 22 2 2007 }}} '''Q2.''' {{{ CONV ← .454 ⌊ .9×CONV×13×14 74 }}} (the weight used in this calculation was 13 stone,) '''Q3.''' {{{ 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 }}} '''Q4.''' {{{ 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 }}} '''Q5.''' b) produces a {{{DOMAIN ERROR}}}. You are trying to add {{{10}}} to the number {{{10}}} and also the characters {{{←21}}}. It is this last bit that doesn't work. c) produces a {{{DOMAIN ERROR}}} - {{{RATE}}} was defined as a string of characters in example (a) and you can't multiply characters and numbers. e) produces a {{{DOMAIN ERROR}}}. You cannot add numbers and characters. {{{#!html
}}} ||<[[../SimpleArithmetic|Back]] || [[../TutorialContents|Contents]] || [[../AplTables|Next]]>|| {{{#!html
}}} ---- CategoryAboutApl