This introductory chapter starts with the programming concept, where we discuss various aspects of programs and algorithms. We consider a simple omelette-cooking algorithm to understand the basic principles of programming. Then, we list the common properties of computer programs, followed by some notes on programming in R, particularly by using the function concept. Finally, matrices and vectors, as well as their representations in R, are briefly discussed.
1.1 Programming Concept
A computer program is a sequence of commands and instructions to effectively solve a given problem. Such a problem may involve calculations, data processing, or both. Each computer program is based on an underlying procedure called algorithm . An algorithm may be implemented in different ways, leading to different programs using the same procedure. We follow this convention throughout this book, where an algorithm refers to a list of procedures, whereas a program refers to its implementation as a code.
A computer program is usually written by humans and executed by computers, as the name suggests. For the solution of a given problem, there are usually several programs and algorithms available. Some of them can be better than others considering the efficiency and/or accuracy of results. These two aspects should be defined now.
Efficiency often refers to the speed of programs and algorithms. For example, one can measure the time spent for the solution of a given problem. The shorter the duration (processing time), the better the efficiency of the program and algorithm used. Note that this (being faster) is quite a relative definition that involves comparisons of multiple programs and algorithms. In some cases, memory required for the solution of a problem can be included in the definition of the efficiency. In such a case, using less memory is favorable, and a program/algorithm using relatively small amount of memory is called to be efficient. For both speed and memory usage, the efficiency of a program/algorithm naturally depends on its inputs.
When a program/algorithm is used, the aim is usually to get a set of results called outputs . Depending on the problem, outputs can be letters, words, or numbers. Accuracy is often an issue when dealing with numerical outputs. Since programs are implemented on computers, numerical results may not be exact, i.e., they involve errors. This is not because programs are incorrect, but because computers use floating-point representations of numbers, leading to rounding errors. Although being negligible one by one, rounding errors tend to accumulate and become visible in outputs. A program/algorithm that produces less error is called more accurate than other programs/algorithms that produce more errors. Obviously, similar to efficiency, accuracy is a relative property. But it is common to call a program/algorithm stable when it produces consistently accurate results under different circumstances, i.e., for different inputs.
When comparing programs and algorithms, there are usually tradeoffs between efficiency and accuracy. Hence, one may need to investigate a set of possible programs and algorithms in detail to choose the best of them for given requirements. This is also the main motivation in programming.
1.2 Example: An Omelette-Cooking Algorithm
Assume that we would like to write an algorithm for cooking a simple omelette and implement it as a program. As opposed to standard ones, these are to be executed by humans. Let us simply list the basic steps.
Gather eggs, crack them in a cup.
Use a fork to mix them.
Add salt, mix again.
Pour butter onto a pan.
Put the heat on. Wait until the butter melts.
Pour the egg mixture onto the pan.
Wait until there is no liquid visible.
This list can be considered as a program, since it is a sequence of commands and instructions to effectively solve the omelette-cooking problem. Note that dividing the third item into two parts as
would lead to another program, even though the algorithm (the underlying procedure) would not change.
For this program to work smoothly, we need a cup, a fork, a pan, and heat. Under normal circumstances, these items do not change. Hence, they can be called the constants of the program. Of course, we can use a bowl instead of a cup to mix eggs. This is perfectly allowed, but constants are considered to be fixed in the content of the program, and changing them means modifying the program itself. Hence, using a bowl instead of a cup would be writing another program, which could be more suitable in some cases, e.g., for large numbers of eggs. Such a modification can be minor (using bowl instead of cup) or major (adding pepper after salt). Making modifications on purpose to change a program, in accordance with new needs or to make it better, can be simply interpreted as programming .
In addition to the constants defined above, we need eggs, salt, and butter in order to cook an omelette. These can be considered as the inputs of the program. These items and their properties tend to change in every execution. For example, the size of eggs will be different from omelette to omelette, but the program above (including constants) remains the same. Note that this is actually the idea behind programming: Programs are written while considering that they will be required and used for different inputs. Types and numbers of inputs are included in the process of programming and often chosen by the programmer who writes the program. In our case, one might consider the number of eggs as an input, which could be used to determine whether a cup or a bowl is required. This would probably extend the applicability of the program to more general cases.
Finally, in the program above, the omelette is the result, hence the output. Outputs and their properties (for example, the taste of the omelette in our case) depend on both constants and inputs, as well as operations performed in the program in accordance with the instructions given.
Now let us try to implement the omelette-cooking algorithm in a more systematic way. In this case, we use some signs to define operations. Let us also list constants, inputs, and outputs clearly. From now on, we use a different font to distinguish program texts from normal texts.