Programming

## 7 Programming

### 7.1 The language

Instructions for Xcas
a:=2; | assignment
input("a=",a); | input expression
textinput("a=",a); | string input
print("a=",a); | output
return(a); | return value
break; | break out of loop
continue; | go to the next iteration
if (<condition>) <inst>; | if…then
if (<condition>) <inst1> else <inst2>; | if…then …else
for (j:= a;j<=b;j++) <inst>; | for loop
for (j:= a;j<=b;j:=j+p) <inst>; | for loop
repeat <inst> until <condition>; | repeat loop
while (<condition>) <inst>; | while loop
do <inst1> if (<condition>) break;<inst2> od; | do loop

Boolean operators
== | test for equality
!= | test for inequality
< | test for strictly less than
> | test for strictly greater than
<= | tes for less than or equal
>= | test for greater than or equal to
&&, and | infixed “and”
||, or | infixed “or”
true | boolean true (same as 1)
false | boolean false (same as 0)
not, ! | “not”

You can extend Xcas by adding desired functions with its built-in programming language. The main features of the language are:

- It is a functional language. The argument of a function can be another function; in which case you can either give the name of a function or the definition of the function. If f(x) := x^2, then function_diff(f) is the same as function_diff(x->x^2).
- There is no distinction between a program and a function. A function returns the value of the last evaluated statement or what follows the reserved word return.
- The language is untyped. Any variable can take on any value; the only different types of variables are global variables, which are not declared, and local variables, which are declared at the beginning of a function.

A function declaration looks like

       function_name (var1, var2, ...) := {
       local var_loc1, var_loc2, ... ;
         statement1;
         statement2;
         ...
       }

The syntax is similar to C++, although many variants are recognized, particularly in compatibility mode. Recall that i is √−1 and cannot be used for a loop variable. The conditional tests are Booleans, which are the results of the usual Boolean operators.

A program can capture runtime errors with a try–catch construction, which takes the form

       try
         {
          block to catch errors
         }
       catch (variable)
         {
          block to execute when an error is caught
         }

For example, the following will catch an error caused by incorrect matrix multiplication:

       try
         { A := idn(2) * idn(3) }
       catch (error)
         { print("The error is " + error) }

### 7.2 Some examples

To write a program, it is a good idea to use the program editor that comes with Xcas, which provides a template and commands helpful for writing programs. You can open this editor with the New Program item in the Prg menu or the Alt+p key.

Consider the following program, which takes two integers and returns the quotient and remainder of the Euclidean division algorithm (like the iquorem function).

       idiv2(a,b) := {
         local q,r;
         if (b != 0) {
           q := iquo(a,b);
           r := irem(a,b);
           }
         else {
           q := 0;
           r := a;
           }
         return [q,r];
       }

If you enter this into the editor, you can test it with the OK button. You can then use the function in the command line; if you enter

> idiv2(25,15)

you will get

[1,10]

You can save it in a file; the name idiv2.cxx would be a good name. You can then use it in later session with the command

> read("idiv2.cxx")

or by opening it in the program editor and validating it with the OK button.

Here are some more programs that you can play with. This first one computes the GCD of two integers iteratively.

       pgcdi(a,b) := {
         local r;
         while (b != 0) {
           r := irem(a,b);
           a := b;
           b := r;
           }
         return a;
       }:;

The second one computes the GCD recursively.

       pgcdr(a,b) := {
         if (b == 0) return a;
         return pgcdr(b, irem(a,b));
       }:;

If a program doesn’t work the way you expect, you can run it in step-by-step mode with the debug command. For more details, consult the Interface item of the Help menu. For example, you can start the debugging by typing

> debug(idiv2(25,15))

The debugger will automatically display the values of the parameters a and b and local variables q and r when executing the program line by line with the sst button.

### 7.3 Programming style

The Xcas programming language is interpreted, not compiled. The run time of an Xcas program is affected by the number of instructions rather than the number of lines.

The speed of a program does not always match up with the clarity of the program; compromises are often necessary. For the most part, the calculation time isn’t an issue; interpreted languages are often used to test algorithms and create models. Full scale applications are written in a compiled language like C++. A C++ can use giac for the formal calculations.

When you are trying to write a fast program, you may want to take into account the number of instructions and the speed of the instructions. For example, it is in general faster to create lists and sequences than it is to program loops. Recall than in Xcas you can find out how long it takes to run a command by entering

> time(command)