📜 ⬆️ ⬇️

Friday format: “You can’t just take it and explain it to a non-programmer ...”

There are concepts that are very difficult to understand even for programmers at the beginning of their journey. And some of them just can not easily and quickly explain the "mere mortals" if they are not mathematics. Usually, such concepts no longer scare people so much after bringing examples from the “ordinary” world.

We tried to find such analogies in 1cloud , so now conversations with non-specialists can become a little more interesting, and you are more likely to reach an understanding (on this topic we wrote a practical guide " How to derail a programmer ").


/ Flickr / dave allen / cc
')

1. Recursion


The expression : “To understand recursion, you must first understand recursion.” But in every joke, as you know, there is some truth.

Let us turn to history: mathematicians used the principle of induction to define a function long before the XIX century, but in 1889 and 1891 Giuseppe Peano introduced a system of five axioms for natural numbers (in 1888 the first version of axioms was formulated by Richard Dedekind), but creation of axioms was fixed for Peano). The fifth axiom of induction was later called primitive recursion. This concept plays an important role in the foundations of mathematics (read more in the work of Robert I. Sohar on Computability and Recursion, clause 2.2).

Today it is difficult to imagine, but there was a time when the use of recursion in programming seemed dubious . So, at the dawn of the sixties, members of the Algol-60 development committee did not agree on this issue. John McCarthy considered recursion an elegant way to teach computers to repeat the execution of the same code with certain changes. He proposed to make the possibility of recursion a characteristic feature of the new language, but it was not possible to come to some kind of consensus on this issue. Scientists were not sure that it would be possible to implement it.

Some members of the committee, for example, Peter Nauru and Adrian van Veingaarden, the idea of ​​recursion seemed tempting. Having studied the draft report of the committee at the end of the 59th, Edsger Dijkstra concluded that practically no one was against recursive calls, but this was not reflected in the document. Then he called Nauru in Amsterdam, asking him to add just one sentence that went unnoticed when the rest of the members signed the project. So the recursion option was introduced.

Recursion can be difficult to understand and novice programmers. For example, John D. Cook, Ph.D. and software developer, says that you should not try to imagine in detail how recursion is performed. Paul Graham in his book on Lisp wrote that usually when defining a recursive function, the programmer does not represent the sequence of calls that is the result of his call.

Another problem is how recursion is explained in textbooks. Blogger and programmer Gustavo Duarte writes that a recursive implementation of factorial is usually shown there, and then it is said that it is terribly slow and can lead to a crash due to stack overflow. It's about as if you were told: “You can always dry your hair by putting your head in the microwave. But be careful, this method can lead to increased intracranial pressure and an explosion of the head. Or you can use a towel. ”

Steve MacConnell, an American programmer, in his book , Perfect Code, says that he fired an employee using recursion. But not everyone agrees with such a tough approach. Thus, the Russian programmer Alexei Michurin (Beeline, Yandex.Monetisation) is sure that in some cases it is impossible to do without recursion, for example, in functional languages ​​like Haskell or such means as XSLT processors. And in the case of the quick quicksort sorting algorithm, which is recursive in itself, recursion will help to formulate its idea more clearly.

But to explain the essence of recursion to non-programmers is not so difficult, because the concept itself is used even in art and is called the Droste effect . In literature, recursion can be found even in Shakespeare: the play, which the main character puts in the play, describes the events of the tragedy Hamlet itself. In the novel “One Hundred Years of Loneliness” by G. Marquez, the life of the Buendía family is recursively repeated from generation to generation, and when the last of the family finishes deciphering the manuscript of the wizard Melchiades (which is the description of the book itself), everything disappears in a hurricane that has flown.

In Russian literature, the recursive structure of works is introduced by Dostoevsky, which even complicates the perception of the text for the reader. An example of a separate recursive element, traditional for Russian writers, is a recursive dream: for example, a triple dream in Lermontov's poem . (For more information about the recursive dreams of Gogol and Dostoevsky, read here : "Son Chartkov" and "Son Svidrigailov").

The theme of multi-level sleep formed the basis of the well-known film The Beginning ( here you can read about a few interesting facts that will help you understand the film). In the story, with each immersion in a dream in a dream, time slows down more and more, until it almost stops at the "limb", where you can live for ages. Another recursion case is the image in the image. It is often used in the visual arts (the most famous example is the work of M. Esher), photography and advertising. And the cases of using recursion in the language are well known to everyone: for example, the song of Maxim Leonidov “I turned around to see if she turned around to see if I did not turn around ...”.

Thus, it is not so difficult to explain to a non-expert what a recursion is. Here you can go to the next stage and try to talk about what cycles are and how recursion differs from them.

2. Nested loops


At the root of nested cycles stands Charles Babbage, the inventor of the first analytical computing machine. His friendship with Ada Lovelace, who is considered the first computer programmer, had a great influence on the development of Ada as a talented mathematician. And it was she who translated the article by Luigi Federico Menabrea (1842), devoted to the analytical machine invented by Charles Babbage. But she completed not only the English translation, but also wrote her comments, which were three times longer than the article itself. In a paper published in 1843 in an English scientific journal, she described a code that could be used in a device to match letters and symbols with numbers, and described the theory of cycles.

To visualize the principle of nested loops, a Turing machine can help a novice programmer. When implementing algorithms, the end of one cycle causes the beginning of another. This continues until the machine is in its original state, which causes it to stop.

Examples of nested loops in the real world are a great many - in fact, any repetitive action - from washing dishes to walking - can be called an nested loop. But let us dwell on some specific analogies. For example, the counter of visits on the site or the speedometer in the car works on the same principle. They consist of 7-8 nested loops with a counter, each of which has a value from 0 to 9. The rightmost number changes the fastest: you see how it increases when a new visitor visits the site or when you drive home. All other numbers change at a slower rate.

NLP also has the term nested loops. The method itself is rooted in the work of the famous American psychotherapist Milton Erickson (Milton Erickson), who specialized in hypnosis. This technique is used for a kind of introduction to trance, to instill a certain idea. The essence of the method - the attachment of history in history. We need to start telling the story, but instead of ending it, start another one. This step should be repeated 3-5 times. In the middle of the last story, you should enter a key idea or motivation, and then complete all the stories told in reverse order, from the last to the first (an example of such a story can be found here in step 3).

3. Parallelism


Beginner programmers sometimes have difficulty working with parallel systems. The main difficulty with their use in sequential languages ​​is to determine when and how to alternate parts of the program that can interact with each other. (For more, read here in the paragraph “What makes Concurrent Software Difficult?”). When working on multi-core processors, the procedure leads to an increase in the execution time of the operation and is complicated by the need for synchronization.

The origins of parallelism can be found even in the work of the same Menabrea on the analytical machine (see the conclusion before the translator’s note). But Carl Adam Petri introduced a more significant contribution to the development of parallel systems in the 1960s, introducing a new concept of parallel and distributed systems applicable to many types of discrete systems in various fields of research (computer science, law, production, transport, chemistry, epidemiology, demography). (For more, read here in the chapter "About Carl Adam Petri"). The first scientific study in the field of parallel algorithms belongs to Edsger Dijkstra, where he proposed solutions to the problem of mutual exclusion.

Outside of programming, such a system was used, for example, in a telegraph message. In this case, a different seal channels. In 1869, G. I. Morozov developed an apparatus for frequency multiplexing of communication lines: the transmission of several messages along one line was carried out with current signals of different frequencies. And in 1872, Emile Bodo (Emile Baudot) created a telegraph apparatus of multiple actions, using a time division multiplexing channel, which made it possible to transmit up to five messages along one line. This method involves the alternation of blocks of information from different streams.

Parallel computing used to be used on the railway, an example of such a system is the level crossing (clause 7.2 in the source ). The main task of the calculations is to distribute a large number of trains in a single track communication system, eliminating the possibility of collision and increasing efficiency. The term “semaphore” has passed from railway communication to programming.

Parallel systems in the real world are not so uncommon, for example, the coordinated work of a team in which different people simultaneously perform different tasks. The effective work of the team is perfectly illustrated by the example of Japanese workers, when 1,200 engineers moved the ground station of the underground into the underground in a few hours.


But the multitasking of an individual also illustrates parallelism. Although some psychologists believe that doing several things at the same time, on the contrary, reduces work efficiency, for a long time multitasking has been a trend. (Read more about the study of British and American psychologists in the introduction to this article). Yes, and we all somehow resort to this technique, even when we draw doodles, talking on the phone.

And of course, you can draw a parallel between the computer and the brain. Unlike the computer, our brain precisely manages to carry out a large number of tasks ( more precisely, about 50) at the same time and fully succeeds in this. But all these tasks are distributed between different groups of neurons. Marvin Minsky wrote the book Society of the Mind, in which he considers the human mind as a collection of separate simple processes called agents. This theory also reflects the foundations of a parallel computing system.

4. Assignment operator


An assignment operator can be confusing for a beginner in programming for two reasons: due to the use of the equal sign and a lack of understanding of the very essence of the operator. The equal sign is not always (and not in all languages) used to denote an assignment operator. For example, in APL, to distinguish between assignment and equality, the assignment is denoted as: <-.

Nevertheless, in many modern languages, the ability to use the “=” sign for an assignment operator is introduced, since it is usually possible to understand from the context what exactly is meant: assignment or association (by the way, Niklaus Wirth considered using the “=” sign as an assignment operator “ bad idea "- and not only because of the difficulty of understanding, but also because of the violation of logic - from his point of view, the introduction of such a designation leads to the emergence of a new logical concept -" coercion to equality ").

Another difficulty is in recognizing the difference between the assignment operator and the copy constructor. By the way, non-programmers can explain the essence of the phenomenon by contrasting these phenomena: the copy constructor is used to create a new object that becomes a duplicate of an existing one, and the assignment operator overwrites the value of an already created object, that is, copies the contents of one object to another.

Additional reading:

Source: https://habr.com/ru/post/314980/


All Articles