Computer programs are step by step instructions that a computer can execute. Program execution will resolve a problem or will have a physical effect. For example printing a message to the computer monitor or moving the arm of a robot.
Computer programs are created using a programming language. In computer industry there are many programming languages with different style of programming.
These languages are artificial languages created specific for computer programming. Artificial languages are much more precise and simple then the general human languages.
In the next example you can see a small Python program …
# compute factorial of n def factorial(n): if n==0: return 1 else: return n*factorial(n-1) # call factorial function and capture result result=factorial(5) print(result)
Computer languages can be categorized by the complexity into:
Close to machine
- 1GL – First Generation:
- 2GL – Second Generation
Close to human
- 3GL – Third Generation Language
- 4GL – Forth Generation Language
- 5GL – Fifth Generation Language
- Machine code is 1’st generation language;
- Assemby is 2’nd generation language;
- Java is 3’rd generation language;
- SQL is a 4’th generation language;
Computer languages are also classified by programming paradigm. This is a style of programming and thinking.
- Liniar programming
- Structured programming
- Procedural programming
- Declarative programming
- Object oriented programming
- Aspect oriented programming
- Functional programming
- Logic programming
Some computer languages are pure and implement a single paradigm and others are hybrid and implement multiple paradigms.
- FORTRAN is a structured language
- Algol is a procedural language
- Python is multiparadigm language
- Haskel is functional language
- SQL is declarative language
- Prolog is logical programming language
See also: Best computer languages
Learn a computer language
To learn a computer language you have to learn 3 things:
- Logic – the reasoning process to resolve problems
- Syntax – the words and rules of the language
- Semantic – the program composition rules
Computer programs may be very smart. The secret behind any smart program is the logic science. Any program need a bit of logic to function otherwise is a very dump program.
The logic was discovered by Greeks and formalized by Aristotle. Logic means thought or reason. Initially the logic was described only by philosophy. However in time a new mathematical branch was developed.
The most important concept in logic is the inference, deduction or implication. This is a conclusion we reach after we study the arguments or premises into a sentence. Inference is a way of thinking and a reasonable deduction.
Logical inference: Using valid arguments into a sentance we can derive a logical conclusion. Sound arguments will produce the same conclusion no matter who makes the arguments or who derive the conclusion.
The inference and reason do not work the same in all cultures. For example a Christian will not reach the same conclusion from a religious sentence as another person that is Buddhist or Mussulman. The words may be interpreted in different ways and lead to different conclusions.
Most sentences are classified in two categories:
- Imperative sentences
- Declarative sentences
It is important to know that logic can’t create good inference from imperative sentences but only from declarative sentences. Imperative sentence is also known as a command from a superior Entity Bean, God or authority like Emperor, Queen or the President.
By making inference from imperative sentences one culture can fall into irrational believes, wrong lows and social injustice. Logic can only work based on facts and solid arguments not on commands.
Fundamental logic rules or peinciples:
Let’s say we have a thing an argument or a premise into a sentance.
- Identity rule: An argument is identical with itself and not something else;
- Contradiction rule: An argument cannot be true and false in same sentance;
- Validity rule: An argument is valid if the truth of premises lead to truth of the conclusion.
Validity and Soundness
A deductive argument is said to be valid if takes a form that makes it impossible for the premises to be true and the conclusion nevertheless to be false. Otherwise, a deductive argument is said to be invalid.
A deductive argument is sound if and only if it is both valid, and all of its premises are true. Otherwise, a deductive argument is unsound.
The propositional logic study the sentences logical arguments. Deductive arguments can be propositions or sentences that can have Boolean values. This logic do not deal with the content of propositions but only with the relation between propositions.
In this rules we use A and B and P to represent logical propositions.
Part of propositional logic are relations: IS, AND, OR, NOT. This relations are very precise.
Fundamental rules of Propositional logic:
These are rules that can’t be broken and are universal rules. Nobody can contradict these rules and these rules are universal in any culture and any language on earth.
- If A is true and B is true then P = A and B is also true.
- If one of A or B is false then P = A and B is false.
- If A or B is true then P = A or B is also true.
Boolean algebra is the branch of algebra in which the values of the variables are the truth values true and false, usually denoted 1 and 0 respectively. This contrasts with general algebra that study other numbers not only the value 1 and 0.
Boolean algebra use symbols for logical operations and values 1 and 0 for truth values.
This and other symbols allow creation of a formalized language for mathematics.
- ∧ for AND
- ∨ for OR
- ¬ for NOT
- A = 1
- B = 0
- C = A ∧B => C is 0
There is a logic that works not only with values true or false but we some values in between. Not everything is black or white but there is a gray area. This may be related to probabilistic values that are values between 0 and 1.
Predicate logic study the things that are considered predicates and relations between the things. This is much more useful and powerful then the propositional logic. The things can be represented using data and values that can be connected with relational operators and symbols.
For example we can study numbers and relation between numbers using predicate logic. A number can be greater, equal or less than other number. Also a number can be different or not equal to other number.
We can use relational operators: >, <, = to show relation between numbers.
This is a simple program that is comparing 3 values and makes a logical deduction or decision. Decision statement in Python is “if”.
# ---------------------------------- # Program used to compare 3 numbers # ---------------------------------- m = input("First number:") n = input("Second number:") o = input("Third number:") # We consider the m to be the max, but we can be wrong max = m max_var = "m" # We verify if our initial asumption is right if n > m: max = n max_var = "n" if o > max: max = o max_var = "o" # We display the result print ("Maximum is: ",max_var,"=", max)
Set of things
To understand the predicate logic we need to first understand a set of things. A set is a group of things that are unique represented and not duplicated. All things in a set can be similar or can have characteristics in common.
Things can belong to classes or category of things that have something in common. For example toys, animals, dogs, propel, furniture, homes. All this can be grouped into sets of things.
Predicate logic study the things and the relations between these things. For example a thing can belong to a set or do not belong to a set. Two sets can contain the same things or different things.
Sometimes we need to make a collection of things and sometimes the thing can be duplicated. In this case we have a list of things not a set because some of the things are not unique.
In mathematics there are 2 significant numeric collection types: Vector and Matrix. In Python we define a “List” that can have one or more dimensions.
In this example we define a vector using a Python single dimension list and then check if an element is member of this list.
v = [1,4,6,12,32] x = input("check this number:") if x in v: print("found") else: print("not found")
A state machine is a special machine that is programmable. It is a logical system that can memorize one or multiple states. States are logical values or numeric values. A state machine can transition from one state to a new state.
When we create programs we create electronic state machines.
A Turing machine was invented by Alan Turing to break the German encryption machine Enigma. Enigma code was broken using the first universal computation machine that was named later computer.
The most complex programs one can make is an artificial intelligence program. For now there are very few companies that can create true artificial intelligence. In the next picture there is a neural network. This was considered for long time a key ingredient for building an artificial intelligence. Now not so much. The statistics and deductive programs are considered more powerful.
In linear programming we can have a sequence of steps that can be executed one by one until the program is finished. The program has a single start point and execute all steps in the program. There is no exception, no jump and no skip. The program always has the same exact effect when is executed. For example HTML is a language that allow linear programming.
In structured programming we create a system. This kind of program is more complex. It’s effect can be different depending on user input, time, interaction with other programs, system events and so on. The idea of structured programming is that a programs can be intelligent.
Using structured programming we can create Turing complete languages. These languages can do several operations that are represented in structured programming with statements.
- Start with an initial state;
- Can ask for input;
- Can change it’s internal state;
- Can make decisions;
- Can execute one sequence of code or another;
- Can repeat a sequence of code one or many times;
- Can have one or more final states;
Depending on the computer language some are implementing sub-programs, sub-routines, procedures or functions that encapsulate a functionality. The main program is orchestrating the execution by translating the user input into calls using parameters or shared-states. Sub-programs have the role “separation of concerns”. It means one sub-program does one only one small thing while the program combine the effects of multiple sub-programs to implement the end-to-end functionality of the system.
The functional programming is a programming paradigm. It is a way of thinking about a function. The function is like an object. That’s the main idea. We do not create classes like in Java. We create only functions. So a functional programming language is much more simple then Java. In a functional programming language a function is “first class objects”.
Characteristics of functional programming:
- A function exist as object that can have properties and can be stored into a variable of type function;
- We can assign the function itself to a variable then use the variable name as a function;
- We can create a function that return a function not a value. This is a higher order function;
- We can create a function that can receive as argument another function that is called a “call-back” function;
- Pure functions do not have side-effects and return a single result.
To cope with restrictions of functional languages have invented a new terminology:
- In a functional language a function can have attributes;
- A function that is created by another function is called closure;
- Execution of a function can be suspended and then resumed;
- A function is deterministic if return exactly the same result for the same arguments;
- A function that is argument to another function is called a call-back function;
- A function that return a different value every time is called is a generator;
- A generator do not return values but yield values each time is called again.
Object Oriented Programming (OOP)
This kind of programming has introduced the idea that data and operations that can apply to data are tight connected. In pure Object Oriented programming we have only two things to define: Native data types and Classes of objects. Each class can hold several attributes and methods. The methods can change attributes.
When can use a class like a collection of methods. Also we can create multiple “instances” of the class. Each instance is called object. An object has same attributes ast the class and same methods. Except for object we can set the attribute values and change attribute values using methods.
To understand better we can compare the class with a cookie cutter. Each cookie we cut is an object created with the same template. So all the cookies are almost the same, except if we put chocolate on them and sprinkle them with different color candies. Then the cookies become a little bit different from each other.
Note: There are advantages and disadvantages of each programming paradigm. Therefore many computer languages implement multiple paradigms.