Lecture 24: Other languages



In this lecture we will compare some languages. In fact, exist thousands of different programming languages. It can easily be said that every application has its own language. Many languages are similar to PASCAL and they are called 'imperative' programming languages. Examples of these are C and FORTRAN. Other languages use a more functional concept, such as MATLAB. Some are memory-oriented (like PASCAL), where variables are in normal memory. Others, like FORTH, are stack-oriented, where variables are stored on a pile (from which only the top variable is accessible). In this lecture we will take a look at several languages.
Most languages share the basic concepts we learned in our PASCAL lectures so far: Other things are more specific to certain languages. These include things like records and pointers.

C

C is a programming language that is very similar to PASCAL. It has the advantage that it is more flexible and very efficient with string operations. For that reason, C is the preferred language for engineers (of telecommunications in particular).
The disadvantage is that C excepts everything and the compiler never helps you. It is of the type "You wrote it. You got it!". For example, it is very heavy on pointers, and, as we have seen, working with pointers has to be done with extreme care.

Examples:
 

PASCAL
C
writeln('Hello World'); printf("Hello World");
a := b; a = b;
if (a=b) then ... if (a==b) ...
while (a<>b) do ... while (a!=b) ...
record struct


FORTRAN

FORTRAN (FORmula TRANslator) is an imperative language, like C and PASCAL which has the advantage that it is very strong in doing scientific calculation. Many calculation routines have over the years been optimized in FORTRAN. Becuase of this, most of the scientific calculations are still done in this language. We have to think about diagonalizing matrices of large dimensions (100x100, etc.).
PASCAL
FORTRAN
writeln('Hello World'); PRINT 20, "Hello World"
a := b; a = b
if (a=b) then ... IF a .EQ. b THEN
while (a<>b) do ... WHILE (a .NE. b)
record doesn't exist


BASIC

BASIC = Beginners All-purpose Symbolic Instruction Code. The advantage of BASIC is that it is a language that is very accessible to the beginning programmer. It allows many things and doesn't care so much about structural programming. As examples: The advantages are also the disadvantages: Because the programs are not compiled they are slow. Moreover, the absence of procedures makes the program rapidly look like spaghetti. Modern versions of BASIC (for example Quick Basic) adpated the ideas of functions and compilation and this also makes BASIC look like any other 4th generation language like the ones described above.

FORTH

FORTH (from FOuRTH generation programming language) is a different language in that it is stack-oriented instead of memory oriented. What this means is that everytime a value is generated in an expression, this value is put on top of a stack. Procedures (like PRINT) can use the top value(s) of the stack. As an example, imagine we want to calculate 3*4+2. In FORTH this would be done in the following way
 
 instruction   stack after instruction   explanation
3
3
put 3 on the stack
4
4, 3
put 4 on the stack
*
12
multiply top two elements of
stack an put result on th stack
2
2, 12
put 2 on the stack
+
14
add top two elements of
stack an put result on the stack
PRINT
  print the value on the top of the
stack and remove it from the stack

In fact, some calculators (notably the Hewlett Packards) use this convention. It is called PostFix notation because the operators are put after the operands instead of in between. Instead of writing "3 * 4", we typed "3 4 *".
Other languages, like PostScript (for printers) are very similar to FORTH.
The advantage is that it is extremely fast. Other languages, more readable by humans, are compiled on a lower level to structures similar to FORTH.


MATLAB, Mathematica, MathCAD, etc.

There are many languages that are specially created to satisfy the needs of scientists in various areas. The basic idea behind all those languages is that a scientist doesn't want to have the feeling (s)he is programming. In extreme cases (for example MathCAD), the 'programming' consists of writing a text document like Word in which equations are present and the computer is executing the equations while we write.
As an example of MathCAD (text written in red italics is output generated by the computer. The rest is written by the user, exactly how it is shown here)
 
This is an example of a MathCAD program.

The Thorian matrix is defined by

( 1 0 1 )
a =  0 1 1

1 1 0

To go to the Oswaldian matrix, we have to invert the Thorian matrix:
b = a-1

This matrix is therefore

( 0.5 -0.5 0.5 )
b = -0.5 0.5 0.5

0.5 0.5 -0.5

We can also include images or other objects. The advantages are obvious; at the end we have a formatted and printable document with calculations. The disadvantages are the low speed and the fact that it is limited to only scientific calculations.

MATLAB is a scripting language, which means that we can, from a command line, call certain small programs by typing their names. We can also create new small programs and safe them in files, so that they can also be called from the command line. As an example: The MATLAB equivalent of the above MathCAD program would be
  a = [
     1 0 1
     0 1 1
     1 1 0
  ];
  b = a^-1
(every line NOT finishing with a semicolon (;) generates as output the value just calculated. In this case the matrix b)
Advantages of MATLAB are

Disadvantages are Mathematica is somewhere inbetween MATLAB and MathCAD.

Java (and C++, etc.)

Java and C++ are so-called object-oriented languages. What this means is easiest to explain in an example. Remember how in PASCAL we can declare a variable s to store our name.
   Var s: string;
Then we can assign a value to this by
   s := 'Peter';
and if we want to show it on the screen we call the PASCAL procedure WriteLn, with s as a parameter:
   WriteLn(s);
We have also learned how to define records that contain information of mixed type. For instance, a record to store my name and my telephone number:
   type r = record
     s: string;
     tel: longint;
   end;
Object oriented programming means that every variable (or function of procedure; therefore we use the more general word object) is a record that can have fields that are variables (like tel and s above) or functions!
We might define an object of type string
  Var s: StringObject;
To assign a value (my name) to the variable we would assign a value to the text field of the record. Something like
  s.text := 'Peter';
To print it, we would call the field 'print' of this object. Printing my name would thus be
  s.print;

The advantage of this is the tremendous flexibility of programming. The disadvatage is obvious: it is much more complicated to write a program. Java has a further advantage that it runs on any computer, independent of the platform. The same (compiled) program will run on a UNIX computer or a Macintosh computer and even on a Windows computer.



Of course, the list of languages doesn't end here. As said before, there are many many more languages. However, with our knowledge of PASCAL, we have a good basis for writing programs in any language. The difference between the languages, is not so big as you might think. That is why Horowitz and Hill in their book "The art of electronics" write about languages

 
Peter Stallinga. Universidade do Algarve, 21 Maio 2004