What is computer programming? Computer programming is the art of solving problems using a programming language. It is both an art and a science in my viewpoint. What then is a programming language? A programming language is a computer language designed to solve problems. Plus ca change, plus c'est la meme chose. This quote is credited to Jean-Baptiste Alphonse Karr. What is this quote stating? The more it changes, the more it remains the same. I think this quote appropriately refers to computer programming. The more computer programming changes, the more it remains the same; the more it remains an art of solving problems. New computer programming languages are still being invented, and yet the only goal behind their invention is the art of solving problems.
If computer programming is about solving problems, why are there so many different computer programming languages? What is the point? One needs to understand the differences between programming languages. A programming language can have a different set of strengths and weaknesses than another programming language. For example, scheme is a dialect of LISP. Scheme is both a procedural and an object-oriented programming language. It is very easy to write iterative procedures and recursive procedures in scheme. A procedure is similar to a mathematical function in that it takes some inputs and produces an output; a mathematical function takes one input and produces one output. Java on the other hand is an object-oriented language. It is not a procedural language. Java has methods which are encased in objects. It is very hard to write scheme style iterative or recursive programs until the latest version of Java. Beforehand, one had to come up with a creative way of doing it.
Let me take a look at a factorial problem in both scheme and Java. Factorial is defined as n! = n * (n-1)!. This definition states that the factorial of a number is that number multiplied by the factorial of the number minus one. Hence, according to the definition of factorials, one can compute it as follow. These are the steps.
3! = 3 * 2!
2! = 2 * 1!
1! = 1 * 0!
The base case is 0! It is always 1. That is the rule. Let me substitute it back into the original formula.
3! = 3 * 2 * 1* 1 = 6.
Let me write a procedure in scheme to show how easy it is to program recursion. I will name the procedure factorial.
factorial(x)(x * ((if (= x 0) 1)
factorial(- x 1)))
One sees how easy it is to write this program in scheme. What’s the point. Well, most people do not like computing huge factorials; It is tedious math that takes a while. A computer can do it much faster than a person given that the procedure is correct. Let me try writing the factorial procedure in Java. First of all, in older Java, one couldn’t write a program as clearly and as simple as scheme for Java. One gets a recursion tree error if one attempts scheme style recursion. Computer programming is about solving problems. One just needs to find a neat way of doing it using for loops in java.
Public int factorial(int x){
int i = x;
Int result = i;
for(i; i < 0; --i){
Result = result * i;
}
return result;
}
Isn’t it neat how I solved this problem in both programming languages in a different way. Java is somewhat similar to C, C++, and C# in syntax; the problem can be solved in the same way. It remains that the more programming languages change, the more they remain the same. Their main goal is the art of solving problems. How many ways can one solve a problem? It depends on the problem.
In part 1 of this article, I talked about computer programming. I said that computer programming is the art of solving problems using a computer programming language. “Plus ca change, plus c’est la meme chose” by Jean-Baptiste Alphonse Karr. The more it changes, the more it remains the same. The more computer programming changes, the more it remains the same. There might be different programming languages, however the goal of computer programming remains the same: solving problems.
In the last part of this article, I demonstrated that a recursion problem could be easily solved in both Scheme and Java. Since the programmatic syntax of Java is similar enough to C, C++, the recursion can be solved using a similar function or method. In this part, I will talk about two different types of programming languages; a procedural programming language versus an object-oriented programming language. Scheme is object oriented and procedural. Java is object oriented. C is a functional programming language. C++ is object oriented as well. What is the big fuss about these types of programming methodologies?
Let’s first define what these methodologies are. A procedural language programming has as its primary focus the art of solving algorithms. An algorithm is a sequence of steps that solves a problem. Hence C, a purely functional language, has variables and procedures, otherwise known as functions. Functions are very much like procedures in scheme. Scheme, on the other hand, is a procedural language. The functions are used to solve algorithms such as the Fibonacci sum. Here is the Fibonacci series.
1, 1, 2, 3, 5, 8, 13, 21, 34 and so forth
To get the next number in the series, one adds the previous two numbers. Furthermore, the series always start with two numbers 1 and 1. Suppose that we are trying to write an algorithm to determine the nth number in the sequence. One can do so programmatically. Here is how to solve this problem in C, C++, and Java.
Public int fibonacci(int n){
int x = n;
int result = 1;
int previous 1;
for(x = 1; x <= n ; x++){
if(x == 1) result = previous;
else if(x == 2) result = result;
else {
int temp = result;
result = result + previous;
previous = temp;
}
}
return result;
}
Here is the solution Scheme: a pocedural recursive solution:
(define fibonnaci(x)
(if (= x 0) 1
(if (x = 1) 1
(fibonnaci(- x 1) + fibonnaci (- x 2)))))
I have just written the Fibonacci sum algorithm in java. If java and C++ are object oriented and Scheme is procedural, how can the same code work for all of these programming languages. Well, anyone who knows C, knows how to tailor this code for Scheme. However that is not the point. The point is that it can be written in C, C++, Scheme, and Java.
What is an object-oriented programming language then? An object-oriented programming language is a programming language in which the focus of all the programming is around objects. An object is a programmatically metaphorical representation of an entity such as a person, place, or thing. Objects in turn can have methods which are actions of the object. They are actions because they stand the possibility of getting executed.
As one can see, there are different types of programming methodologies. Some programming languages are very instrumental in solving recursive problems. Other programming languages, are less efficient at solving recursive problems; as a result, they solve the recursive problem iteratively. Scheme solves recursion algorithms both recursively and iteratively; however, Java, C++, and C solves them iteratively. It remains true that computer programming has as its focus the art of solving problems. Would it be true that any problem that can be solved in one programming language can be solved in all programming languages. The answer remains to be proven.
[1] Harold Abelson, Gerald Jay Sussman, and Julie Sussman, Structure and Interpretation of Computer Programming,/i> 2nd ed (Cambridge, MA: The MIT Press, 1996), 217.