[Next] [Up] [Previous]
Next: User-Defined Procedures Up: Appendix: Programming in POP-11 Previous: Variables

Assignment

To store a list in the memory that the new variable refers to, we use the POP-11 assignment command, indicated by an `assignment arrow', written ->. To store the list [1 Acacia Avenue] with the name myinfo, we give the command

[1 Acacia Avenue] -> myinfo;

Now whenever we refer to myinfo again, the POP-11 system plucks out the stored information. For example, the following command prints the data object with the name myinfo:

myinfo =>
** [1 Acacia Avenue]

If we store something new in the variable, then the old information that was stored there is simply lost: the chunk of computer memory for the variable gets changed, and there is no record of what was there before. So, the command below changes the information in myinfo from the list [1 Acacia Avenue] to the word "cats":

"cats" -> myinfo;
myinfo =>
** cats

In fact you can have any POP-11 expression on the left of the assignment arrow, and the result will be stored in the variable on the right of the arrow. So if you write

12 / 3 -> myinfo;

then myinfo thereafter refers to the number 4.

You can regard the chunk of memory labelled by a variable as if it were elastic: you can store a list of any length in it without worrying about the details of how this is done. In fact POP-11 can lump together the primitive units of memory (the empty and full boxes of section 1.2) to store numbers, words, booleans, or lists of any length in the section of memory associated with one variable. One of many tasks of a programming language like POP-11 is to relieve the programmer of having to think about how memory is allocated. POP-11 also `remembers' what type of data is stored: it records whether myinfo refers to a list, a word, a number, or a boolean. This can be useful. If you accidentally try to multiply a number by a list, for instance, POP-11 will give a mishap message to say that this is an illegal operation. The reason why variables are so very useful is that they enable us to write programs without our knowing in advance what information the programs are going to work with.


[Next] [Up] [Previous]
Next: User-Defined Procedures Up: Appendix: Programming in POP-11 Previous: Variables

Cogsweb Project: luisgh@cogs.susx.ac.uk