Before discussing additional TUTOR calculational capabilities, let's review briefly those aspects which have been covered so far:
Now let's look at additional TUTOR calculational capabilities.
While many important functions such as In(x) and log(x) are built-in to the TUTOR language, it is frequently convenient to define your own functions. To take a simple example, suppose you define a cotangent function:
Then, later in your lesson you can write:
and TUTOR will treat this as though you had written:
Such use of functions not only saves typing but improves readability.
CAUTION: In defining a function, the arguments must not be already defined. For example, the following definition will be rejected by TUTOR (with a suitable error message):
This must be rewritten as:
or anything similar. A function definition may involve previously defined quantities on the right side of the “=” sign, however. You might have:
In this case you might have a -calc- that looks like:
and this would be equivalent to:
Sometimes it is convenient to define “functions” that have no arguments:
Note that “prod” depends on two previous definitions, each of which (in turn) depends on the definition of “r”. There is no limit on how deep you can go in definition levels. The unusual definition of “trans” permits you to write an unusual -calc- (where the assignment is implicit in the definition of “trans”):
Essentially anything is a legal definition. The only rule is that the definition make sense when enclosed in parentheses (since a defined name when encountered in an expression is replaced by its meaning and surrounded by parentheses). This means that you cannot define “minus=-” because (-), a minus sign enclosed in parentheses, is not permitted in an expression. On the other hand, “minus=-1” is all right because (-1) is meaningful.
A function may have up to six arguments. Here is a function of two arguments:
This means that modulo (17,5) in an expression will have the value 2; the “int” or “integral part” function throws away the fractional part of 17/5, leaving 3, so that we have (17-5×3)=(17-15)=2. This modulo function, therefore, gives you what is left over in division of “N” by “base”.
Here are a couple of other examples of multi-argument function definitions:
The minus sign appears because logical true is represented by -1. If you have “big(x+y,z)” in an expression, with (x+y)=7 and z=3, this expands to:
which reduces to -[7×(-1)+3×(0)] which is 7. So our “big” function picks out the larger of two arguments.
It is often important to be able to deal with arrays of data such as a list of exam scores, the number of Americans in each 5-year age group together with their corresponding mortality and fertility rates, a list of which pieces are where on a chess board, or the present positions of each of several molecules in the simulation of the motion of a gas.
Suppose we have somehow entered the exam scores for twenty students into variables v31, v32, v33 . . . up to v50. Here is a unit which will let you see the score of the 5th or 13th or Nth student:
(The -wrongv- rather than -ansv- makes it easy to ask another question.) The new element here is the “indexed variable”:
which means “evaluate 30+N, round to the nearest integer, and choose the corresponding variable”. For example, if N is 9, v(30+N) is v(39) or v39. If N is 13.7, v(30+N) means v44.
We might list and total all the scores:
As usual, it is preferable to define a name for this data, such as:
in which case we would write our last unit as:
Due to the special meaning attached to “v(expression)” you must exercise some care in using a variable named “v”, in that you must write “v×(a+3b)” and not “v(a+3b)” if you mean multiplication. We will see later that the same restriction applies to the names “n”, “vc”, and “nc”. This restriction does not apply to students entering algebraic responses, where “v(a+3b)” is taken to mean “v×(a+3b)”. Students can use indexed variables only if they are named (as in “scores” in the above example). Such definitions must, of course, be in the “define student” set.
Suppose you have three sets of exam scores for the twenty students. This might conveniently be thought of as a 3 by 20 (“two-dimensional”) array. Suppose we put the first twenty scores in v31 through v50, the second set in v51 through v70, and the third set in v71 through v90. It might be convenient to redefine your array in the following manner:
Then, if you want the 2nd test score for the 13th student, you just refer to scores (2,13) which is equivalent to v(10+40+13) or v(63). If you wanted to display all the scores you might use “nested” -do- statements:
Unit “column” is done three times and for each of these iterations, unit “rows” is performed twenty times.
There is an alternative way to define our array:
Then our unit “rows” would look like:
The indices specifying which test is for which student are implicit. This form is particularly useful when you have large subroutines where “i” and “j” are fixed and it would be tiresome to type over and over again “scores(i,j)”. Just set “i” and “j”, then -do- the subroutine.
It is frequently necessary to initialize an entire array to zero. One way to do this is with -do- statements:
A simpler way to accomplish the same task is to say:
You simply give the starting location (the first of the 60 variables) and the number of variables to be cleared to zero. As another example, you can clear all of your variables by saying:
Not only is the -zero- command simpler to use, but TUTOR can carry out the operation several hundred times faster! TUTOR keeps a block of its own variables, each of which always contains zero. When you ask for 150 variables to be cleared, TUTOR does a rapid block transfer of 150 of its zeroed variables into your specified area. This ultra-high-speed block transfer capability can be used in other ways. For example:
performs a block transfer of the 25 variables starting with v10 to the 25 variables starting with v85. In this way you can move an entire array from one place to another with one -transfr- command, and at speeds hundreds of times faster than are possible by other means.