Southend Linux User Group

Programming

A preface to programming

Before looking at any specific programming language, there is some jargon we need deal with first. Today there are many different programming languages, here are just a few, So what’s different about them?

Javascript, Python, Java, C++, PHP, PROLOG

Lets start by asking the slightly easier question, what is the difference between a programming language and English? (or for that matter, whatever your native tongue happens to be).. Have you ever picked up an instruction manual and ended more confused than when you started. The English language has a particularly rich and large vocabulary. It is estimated that around the age of 5 a child may know some 5000 words and by the time they are an adult that number has risen to between 12,000 – 20,000.
Words can have multiple meanings (homonyms), depending on the context they are used in. Words, phrases, or sentence can be ambiguous. Consider the following,

Time flies when Jack takes Gill out on a date because he says, she is as nutty as a fruit cake, saying things like, no wise fish would go anywhere without a porpoise and fruit flies like a banana, but my favourite fruit is a date.

By drawing on our experience and knowledge, we can generally make sense of this, assigning the appropriate meaning to a word in the context it is used (e.g.date, flies ), recognising English idioms, word play and so on. Things get even worse when you throw spelling and mistakes in punctuation, as the next passage illustrates. The human brain is very good at glossing over errors.

Can you raed tihs? Olny 55 plepoe out of 100 can.

i cdnuolt blveiee taht I cluod aulaclty uesdnatnrd waht I was rdanieg. The phaonmneal pweor of the hmuan mnid, aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it dseno’t mtaetr in waht oerdr the ltteres in a wrod are, the olny iproamtnt tihng is taht the frsit and lsat ltteer be in the rghit pclae. The rset can be a taotl mses and you can sitll raed it whotuit a pboerlm. Tihs is bcuseae the huamn mnid deos not raed ervey lteter by istlef, but the wrod as a wlohe. Azanmig huh? yaeh and I awlyas tghuhot slpeling was ipmorantt!

A computer programming language is the complete opposite of this. It has a very limited vocabulary, typically around 100 words. Each word can only have single meaning and there can be no ambiguity. Furthermore the syntax (punctuation if you like) of a program must strictly adhere to the languages rules. A computer will parse (scan) a program and anything that doesn’t conform to the rules will throw an error, or perhaps worse ignore it. The only knowledge a computer has, is the knowledge contained in program itself, it cannot draw on previous experience, or make assumptions. It must be a precise set of instructions to solve a particular problem, the details of which has first been worked out by a the programmer.

Until you get used to these constraints, codifying what appears to come naturally, is a daunting task. For example if I gave you a random list of words and ask you to put them in alphabetical order, you would have no difficulty doing this. Now if I asked you to initially write down, in plain language, the procedure you followed, you will find this much more difficult, despite having just done it without thinking. It may help you to know that the instructions for all procedural languages fit into just three structures.

  • Sequence – e.g. a shopping list or an HTML page.
  • Selection – i.e. make a decision if (this condition is true) { do this } else {do something different}
  • Repetition – i.e. repeat a set of instructions while some condition remains true.

To illustrate just how easily you, or a computer program, might be derailed, is suppose the list of words I give were either Russian or Greek. Now unless you are familiar with these alphabets, your stumped; to use a another idiom.

Lets turn our attention back to the examples of program languages listed above, what’s different about them? There are many different ways they may be categorised, there is no one correct answer. Languages may initially be divided into two types,

  • procedural (imperative) languages, or
  • declarative languages

With the exception of PROLOG, all the other languages are procedural, and as the category implies, they are designed to follow a set of instructions given to them by the programmer. Today this is by far the must common type of program language you will encounter.

Now is not possible to write a procedure to solve every problem you might have. For example, how would you write a procedure to play chess, that could be blindly followed, and win? Claude Shannon, who did a lot of the early work into information theory, calculated the number of possible chess games is 10120. That’s 10 followed by 120 zeros! Other examples are a computer program to drive a car safely, or undertake a medical diagnosis of a sick patient. Despite the complexity of these task, humans can play chess / drive cars / undertake a medical diagnosis. In fact some people perform these tasks very well, so how do they do it?

Declarative program languages, like PROLOG, (another is LISP) are an attempt to address these kind of problems. They are often labelled A.I (Artificial Intelligence) programs, because they appear to be behaving intelligently; which is just an illusion. Some might even argue that human intelligence is just an illusion to, and that we are just programmable machines, but that’s another story.

Declarative programs are characterised by a list of rules. Taking the example of a chess program, I could list the rules which describe how to move of the individual pieces. Next I could write down rules which determine whether a move is legal or not. At this point I could run my program, and for any given position, get a list of possible moves I could make. It might be none, if I’ve just been check mated! Otherwise how do I decide which is the best, or right move, to make is the tricky bit. Ask any group of experts what rules they use, and each will give different answers, that will be incomplete, because they simply don’t know how they do it either.

Focusing in on the procedural languages listed above, they are all high level languages, meaning they have built in, a degree of knowledge, for example they know how to sort lists of words, or numbers, and perform arithmetic calculations, so you don’t have to reinvent the wheel. Conversely using a low level program, means you must write your own procedures to carry out these tasks, before you actually got around to the problem you were trying so solve.

Having written a plain language set of instructions to solve a given problem the next task is to convert this into the grammar (syntax) of your target language. You have to get this right, the computer won’t say, Oh you’ve made a spelling mistake, left off a comma, or set of quotes I’ll fix it for you. No it will just stop and say the dreaded words syntax error, other something equally nasty. You must then debug (fix) the error before the program will run. The bad news is the syntax of each language is different, the good news is, that between the modern procedural languages I’ve listed above, attempts have been made to minimise the differences.

The next sub category is whether the program is interpreted or compiled. C++ is complied, while PHP, Python, Java and javascript are interpreted. Any high level program has to be translated in a series of 0’s and 1’s, (or if you like true/false, On/Off) because that is all a digital computer understands. A compiler will attempt to translate the whole program (the source) in one go, before it is run to identify any errors in coding. The compiled version is known as the object. That’s not to say that when you run the program it won’t encounter errors. An interpreted program on the other hand will translate the next instruction, as the program is running. Consequently interpreted programs run slower than compiled programs, however today for all but the most processor intensive programs the difference is usually negligible. It is also possible that some branches in a program may not get interpreted at all, until no day a set of circumstances arises that forces the program down a little used branch.

Further classification beyond this point is somewhat academic, so a quick word about PHP and javascript. These are usually classified as scripting languages, meaning they are embedded in, and rely on another application to function. javascript is embedded in the browser (Firefox, Opera etc) while PHP is embedded in a server (Apache for example).

Author: Alan Campion - Page reference: 3061
Last modified: Alan Campion - 2015-01-27