1.4 Basic XploRe Syntax


1.4.1 Operators

In the introductory example in Section 1.1, we have only considered two numbers 1 and 2, and the addition operator +. Other standard operators are available:

These five basic operators are ordered by precedence rules, which are the same as in pencil and paper calculus: entering in the input line the arithmetic expression
  2*2+3
gives in the output window
  7
since the operator * has precedence on the operator +. Thus, the computation 2*2 is carried out first and gives 4 as result, which is added to 3 to yield 7.

The exponent operator $ \wedge$ has the highest level of precedence. Next, the operators * and / have the same order of precedence, and have precedence over the operators + and -. As in standard paper and pencil calculus, you can force these precedence rules by using parentheses: the command line

  (3*4)^2
gives
  144
as the expression (3*4) is enclosed in parentheses, and is then evaluated independently from the other operations. The outcome of this operation, 12, is squared to obtain 144.

As in pencil and paper calculus, parentheses could be nested, although left and right parentheses must match. Assume that you enter the instruction:

  (1+(3*4)^2
which is incomplete since the two left parentheses are matched by only one right parenthesis. This incomplete instruction ends in a error message, i.e. the syntax analyzer of XploRe , or parser, has detected that something went wrong and gives the cause of the mistake.


1.4.2 Variables

In the previous subsection, we have considered numeric expressions which -- once evaluated -- are ``lost'' since they are not stored anywhere. If you need to keep the result of a numeric expression for further calculation, you can store it in a precise location by assigning it to a variable.

The assignment is done with the assignment operator denoted by the symbol = which means ``equal''. For example,

  a = 2*3
assigns the value 6 to the variable a.

We assign a variable by either storing in it a number or the content of another variable. The following command

  b = a
assigns into the variable b the content of the variable a. We can verify this by displaying the content of b by typing its name:
  b
prints
  Contents of b
  [1,]        6
in the output window.


1.4.3 Variable Names

The name of a variable should be a string of alphabetic or numeric characters: a, b, product, result are possible variable names. Not allowed for the use in variable names are spaces (blanks) and underscore symbols (_).

The XploRe parser is case sensitive, i.e. the variables a, A, as well as result, Result, ResulT and RESULT are considered as distinct. Thus, assigning the value 5 to the variable A

  A = 5
does not affect the content of the variable a which still contains the number 6. We display the content of both A and a by typing
  A
  a
which gives in the output window
  Contents of A
  [1,]        5 
  Contents of a
  [1,]        6

Lastly, some labels which represent standard constants in XploRe are protected and cannot be used as variable names. These are pi for the constant $ \pi=$ 3.1415926... and eh for the constant $ e=$ 2.7182818....


1.4.4 Functions

The environment XploRe provides several hundreds of functions for data handling, statistical data analysis, graphics and user interaction. There are two types of functions:

Among the commands, you find all important mathematical functions, such as

2627 log , 2630 log10 , 2633 exp logarithm and exponential functions
2636 sin , 2639 cos , 2642 tan trigonometric functions
2645 abs absolute value
2648 sqrt square root
2651 floor , 2654 ceil , 2657 rint rounding functions

Since XploRe is a matrix-oriented language, applying a mathematical function to a data matrix means that the function is applied to all elements of this matrix. For example,

  x=#(1,2,3)  ; vector of 1, 2, and 3
  log(x)
returns
  Contents of log
  [1,]        0 
  [2,]  0.69315 
  [3,]   1.0986
i.e. the 2664 log has been applied to all elements of the vector x. You find more information on available functions for matrix handling and data analysis in Descriptive Statistics (2) and Matrix Handling (16).

Quantlets are used in the same way as the built-in commands, except for the fact that a quantlib has to be loaded first. For example, the function 2667 mean is a built-in command while the function 2670 median is a quantlet from the xplore library. If you want to compute the median of a data vector, you first have to load the xplore library:

  library("xplore")
  x=#(1,2,3)  ; vector of 1, 2, and 3
  median(x)
To find out which library a function belongs to, consult the APSS help file of this function.


1.4.5 Quantlet files

Any file which consists of XploRe instructions is called a quantlet. Quantlets are saved as text files using the file extension .xpl.

We use the term quantlet for two types of .xpl files:

All quantlet files can be loaded by using the Open item from the Program menu. This opens an editor window containing the quantlet code. We execute the quantlet by clicking the Execute item in the menu bar or by entering $ <$Alt E$ >$.

If the quantlet contains a code to be executed line by line, the execution is immediately started. If the quantlet defines a procedure, an additional instruction to carry out the procedure is required. We recommend to study Quantlets and Quantlibs (17) for using such procedure quantlets.