You get a bonus - 1 coin for daily activity. Now you have 1 coin

Styles and programming techniques

Lecture



Introduction

Programming in languages ​​appeared simultaneously with computers. Konrad von Zuse, who built the world's first series of software-controlled computers (Germany, 1938-1944), created the Plankalkul language for recording programs. Qualified employees wrote programs in this language, and the technicians then manually converted them into machine codes.

Now the most widely used traditional languages . These include FORTRAN, Pascal, C / C ++, Ada, Java, etc.

This combination of traditional languages ​​creates the erroneous impression that programming is almost the same in all languages. To dispel it will help this course, which is dedicated primarily non-traditional languages ​​and non-traditional paradigms in programming.

But first you need to put in order the knowledge and programming skills you already have.

The purpose of this lecture is to introduce the learner to the zoo of systems and concepts of programming. The practical goal of the learner is to acquire the ability, even without first learning the language of the traditional type, to see in it common constructions and, accordingly, to understand the programs written in it.

Task 0 . Copy the previously written program from 100 to 200 lines to an unknown traditional language (choose the first one available from the list of unknowns: Ada, Modula-2, Algol 68, Simula, FORTRAN). It is forbidden to learn a language in detail. Review the description and examples, use first of all the translator help system.

Comparison of programs in different languages

Let's start with an example. For all given texts of the programs, at the performance, there corresponds an action consisting in printing out the line “Pryuvet Volku!”.

  / * C language. * /
 #include <stdio.h>
 int main (void)
     {printf ("Privet the Wolf!");
      return 0;} 
1.1.
  // java
 public class HelloWorld {
     public static void main (
      String [] args) {
         System.out.println (
          "Good morning to Wolf!");
 }} 
1.2.
  (*Pascal*)
 program First (Output);
 begin
     writeln ('Privet Volk!')
 end. 
1.3.
  comment Algol 68 comment
 begin
     println ('Privet Volk!')
 end
 comments Russian Algol 68 comments
 Start
     print ('Merry Wolf!')
 end
 comment Two more views comment
 (println ('Dear Wolf!'))
 (print ('Privet the Wolf!')) 
1.4.
  Lisp (program file execution mode):
 (Print "Privet Volk!") 
1.5.
  Lisp (interactive mode):
 [1]> (progn (setq x "Privet Wolf!") X) 
1.6.
  Refal
 $ ENTRY GO {= <Prout 'Privet Volk!'>;} 
1.7.
  Prolog
 : -Print ('Privet the Wolf!'). 
1.8.

Compare all these programs. What do they have in common?

  1. All of them are presented in the form of texts: sequences of characters placed on several lines.
  2. Each language has the concept of a string (a sequence of characters of unfixed length). Strings are drawn up in all languages ​​in approximately the same way: using quotation marks as framing characters. True, the type of quotes varies from language to language.
  3. Each of these programs has a construction, the execution of which leads to the printing of the string.
  4. All of them, when they are executed, do the same thing: they print the string "Privet Volk!".

What are their differences? In the record, as well as in the rules of construction. Thus, the conclusion suggests itself that, having mastered one programming language, you can understand the texts of programs in most other languages ​​almost as easily as an educated Russian can read Ukrainian or Polish (which is a little more difficult) text 1 .

It would seem that all these programs, of course, after converting each text into executable code 2, lead to the same machine program executed by a computer. But the actual situation is slightly different.

Work of the program system on the text of programs

Now let us analyze in turn how each text of the programs discussed in the previous paragraph is prepared and executed.

Explanation of the program 1.1.

  / * Language C: * /
 #include <stdio.h>
 int main (void)
   {printf ("Privet the Wolf!");
    return 0;} 

Line

  #include <stdio.h> 

reflects the fact that the written text of the program should be expanded by inserting instead of this line text, called stdio.h, and already such extended text is served to the translator (one of the rules for the formation of texts of programs in C). Thus, stdio.h is a translation period library. In fact, stdio.h contains everything that is needed to organize data input and output, describing the components of the runtime library.

The text after #include <stdio.h> is a description of the function without parameters, which returns an integer (its header is int main (void)), and prints the string:

  printf ("Privet Volk!") ;. 

After processing this text by the translator, in particular, the library function of the printf runtime is added, the description of which is taken from stdio.h.

Explanation of the program 1.2.

  // Java:
 public class HelloWorld {
   public static void main (
    String [] args) {
     System.out.println (
      "Good morning to Wolf!");
              }} 

Line

  public class HelloWorld 

Java text indicates that the program is a public (accessible to all) class called HelloWorld. This class can be accessed to execute the actions it contains. Inside the HelloWorld class, the static void main function is defined, with which the computations begin. And inside it, a call is made to the system line output facilities contained in the System.out class:

  System.out.println ("Privet Wolf!"); 

This call is made from the declared function main.

Explanation of the program 1.3.

  (* Pascal: *)
 program First (Output);
 begin
 writeln ('Privet Volk!')
 end.
 (* PASCAL *)
 PROGRAM FIRST (OUTPUT);
 BEGIN
 WRITELN ('Privet the Wolf!')
 End. 

The last two texts in the standard Pascal 3 , even in terms of syntax, are the same program. From this it is clear that in the language there are upper and lower case letters. This feature dates back to the use of primitive printing devices for which there was no such distinction.

The ability to print something for the Pascal language is determined by specifying the Output in the program header, which enables the corresponding runtime library and initializes it.

Explanation of the program 1.4.

  Algol 68: 
 begin
 println (`Privet Volk! ')
 end
 comment Russian Algol 68 comment
 Start
     print (`Privet Volk! ')
 end
 comment Two more views comment
 (println (`Dear Wolf! '))
 (print (`Privet Volk! ')) 

Algol 68 demonstrates four texts of the same program. The language also contains variants of the notation for national alphabets (compare the first and second texts), and the possibility of cursive writing (compare the first and third texts).

For this language, the existence of a standard entry and conclusion that surround the written text is postulated. It is believed that the source text for the broadcast is what will result from the combination of the text of the introduction, the text written by the programmer, and the text of the conclusion. There is an analogy with the C language #include, but the name of the file that should expand the written text is not explicitly specified.

Explanation of the program 1.5.

  (PRINT "Privet Volk!") 

A Lisp program is a PRINT function with the argument "Preach the Wolf!". The calculation of this function is the so-called S-expression, representing the argument of the function itself. In this case, it is "Privet the Wolf!". When calculating PRINT, a side effect occurs, an action that accompanies the acquisition of a value. In this case, it is the printing of the function argument, i.e., the desired action. The above program prints the line twice: the first time the specified side effect is performed, the second one because of the following reason. A Lisp program always completes its calculations by printing out the value of the function obtained as a result. S-expression "Privet Volk!" and that is the result.

Explanation of the program 1.7.

  $ ENTRY GO {= <Prout 'Privet Wolf!'>}; 

The program at Refal is a Go function. This function works with the field of view, which can no longer be directly represented as a collection of cells in the calculator. It checks that the field of view is empty, and substitutes instead of the empty expression what comes to the right of the equal sign: a call to a standard function that prints a line and again clears the field of view. There are no more functions left in the field of view, the program finishes its work, and since the field of view is empty, nothing else is printed.

Explanation of the program 1.8.

  : -Print ('Privet the Wolf!'); 

A Prolog program is a goal that must be achieved. In a typical situation, in the field of view there are also data necessary to achieve the goal, in the simplest case such data is not needed. A standard function is called that prints the string and disappears. Unmet goals no longer remain, the program ends.

In this example, you can see common features of the languages ​​Prolog and Refal. Both of them deal immediately with complex data and are not directly related to the physical structure of computer memory.

We see that the actions prescribed by the language achieve the same goals in completely different ways. What is the reason? Each language defines its own calculation model . Sometimes these models are quite close, despite significant differences in the pictorial means of languages. For such languages, the programmer essentially writes the same thing, and the functions of the programming systems are close. Differences in design are related, in particular, to how the program relates to its environment and how actions are common for all programs. That is why we have included the most common languages ​​with similar computational models in the list of traditional languages.

Significant differences in computing models arise in the case of different data devices with which programs work (compare, for example, C and Refal). But it is worth remembering that the same model of computing on different computers and in different operating environments is implemented differently. Bit grids, ways of representing numbers and methods of calling procedures can be different. Perhaps the consideration of the operating environment for software portability is implemented only in the language of Ada, which is the standard for the US Department of Defense.

created: 2016-05-05
updated: 2021-03-13
104



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Programming Styles and Techniques

Terms: Programming Styles and Techniques