Uploaded by Anjan K. Lakhan

cape-notes-unit1-module-3-content-11

advertisement
Syllabus Focus: Unit 1 Module 3 Content 11
Specific Objective 11: explain the concept of programming;
Content: Development of computer programs; stages in programme development;
programming paradigms; examples of programming languages.
Programming languages come in many forms or 'paradigms'. Each form of language offers
advantages over other types and each one of course has some limitations as well.
Furthermore there are many languages available within each form. Sometimes the choice of a
particular language is personal preference and familiarity or sometimes it is imposed by the
company you are working for.
You need to know the main classes of programming and why you would use one type over
another.
The main classes of programming languages are





low level languages,
object-orientated languages
declarative languages
procedural
functional languages.
Low level language concepts
A low level language is one whose programming statements are geared towards a particular
CPU family, such as the x86 family of processors. Low level languages are almost (but not quite)
machine code.
'Assembly language' is an example of a low level programming language.
Chip makers such as Intel and ARM provide programmers with an Assembly Language with
which to code their particular CPU.
Some features of Low Level languages include
 They are CPU specific, making direct use of internal registers
 'Mnemonics' are used as programming code such as MOV or ADD
 Labels are used as reference points to allow the code to jump from one part to another.
Pros.
 Low level languages are excellent for close control of the CPU, for example many device
drivers are coded in assembly language.
 They can be very efficient. Well-optimised code written in a low level language can be
made to run very quickly compared to other programming paradigms.
Cons
 They are difficult to use as the programming commands can be quite obscure
CAPE NOTES Unit 1 Module3 Content 11
1
 A good assembly language programmer needs to know a lot of detail about the internal
structure of the CPU - e.g.its registers and memory management methods
 Low level languages produce the least portable source code.
Assembly language looks like this:
.MODEL SMALL;
.STACK;
.CODE;
mov ah,1h; moves the value 1h to register ah
mov cx,07h;moves the value 07h to register cx
int 10h;
Procedural concepts
With procedural languages, you code specific instructions for the computer to carry out - it is all
about the 'do-this, then-this, then-this' style of programming.
This is one of the commonest programming paradigms in use.
The main points to know about procedural programming languages are:
Imperative Languages
They are sometimes called imperative languages. Imperative means 'to give orders, or
instructions' and so procedural languages are all about telling the computer what to do, step by
step.
Sequential instructions
All imperative languages lay out their instructions in sequence.
3rd generation languages
Procedural languages are examples of 3rd generation languages.
It is now many decades since the first computer language was created. Third generation
languages make it easier for people to read and understand the code. This is done by using ideas
such as naming variables and using functions or subroutines to partition the code into
manageable chunks.
Examples of Procedural languages are
C, Pascal, FORTRAN, COBOL
A typical clip of source code written in a procedural language:y = 1224;
for (x = 0; x < 100; x++) {
z = y + x;
}
CAPE NOTES Unit 1 Module3 Content 11
2
In this clip, a variable y is being set, then the code enters a loop. So the procedural language is
precisely defining what the computer should be doing step by step.
As a comparison, SQL database language is a 'non-procedural' language. A simple SQL query is
shown below.
CREATE TABLE Parts
SELECT * From Customer
Here, the first instruction tells the database management software to create a table called Parts,
then it makes a request for all records from the Customer table.
The SQL programmer does not need to know the precise detail of how a table is made or the
details behind pulling down all records from a table.
Pros
 Excellent for general purpose programming
 Many books and references available on well-tried and tested coding algorithms - no need
to re-invent the wheel.
 Good level of control without having to know precise target CPU details - unlike low
level languages
 Portable source code - use a different compiler to target a different CPU
Cons
 As there are so many procedural languages, a programmer tends to have to specialise in a
particular language in order to get work. A COBOL specialist has a different clientele to a
'C' specialist.
 Need to be very precise and knowledgeable about programming instructions, and so a
fully de-bugged working program takes more time to put together compared to fourth
generation languages such as Simulink.
 Not as efficient as hand-crafted source code written in a low level language
 Poor at handling fuzzy conditions as found in Artificial Intelligence applications - unlike
declarative languages such as PROLOG.
Object oriented concept
An object-oriented programming (OOP) language makes use of the idea of classes and objects.
A program written in an object oriented language will be made up of a number of classes and
objects. These classes and objects are manipulated through their internal methods.
Before OOP languages came along, procedural languages, such as 'C', treated data and the
programming instructions that act upon the data as separate things.
This separation made it very easy to write faulty code such that one part of the software would
over-write data at the wrong time.
CAPE NOTES Unit 1 Module3 Content 11
3
A small non-OOP clip:
global variable X;
function A() {
X = 20;
}
function B() {
X = 1000;
}
In the clip above, one part of the main software may call function A to set the global variable X,
but then another part of the software could call function B at the wrong time and X gets
unintentionally changed. De-bugging this kind of problem can be difficult as the conditions that
made it happen can be very obscure.
So the main idea of object oriented languages is to gather the data and all the methods
(functions) that act upon that data into one entity called a 'class', so making data hiding, code reuse and maintenance much easier.
Pro
Con
 OOP makes it easier to provide working code. A class can be fully tested and released for
other coders in the team to use
 Classes can be treated as 'black boxes'. Other coders do not need to know how the class
works internally. They just need to know how to manipulate it through its methods.
 There are many 'design patterns' available, developed over the years, that solve common
programming tasks. A coder can pick up a design pattern for perhaps an user interface and
begin coding straight away.
 Code re-use. Easy to move a class from one application and re-use it on another project
 Code portability. Just get the right compiler for the target CPU and the source code can be
run on an entirely different hardware platform
 Steep learning curve, becoming proficient in an object-oriented language can take a long
time as it is more complicated than a standard procedural language.
 Complex - it needs skill to craft efficient and flexible classes.
 Not as compact and efficient as writing code directly in a low level language
Example of OOP languages
 JAVA created by Sun Microsystems around 1995 and is now widely used on a number of
hardware platforms from web servers, back-end business applications and even mobile
phones. Code is run on a 'Java virtual machine'.
 C++ around since 1983, it has become one of the most popular OOP languages available.
It has some low level language features as well as OOP high level features. It is widely
used for games development, embedded software, device drivers as well as standard
coding work.
CAPE NOTES Unit 1 Module3 Content 11
4
Declarative language
A declarative language is non-procedural and very high-level (4th generation). This means the
programmer specifies what needs to be done rather than how to do it (a bit like Captain Pickard's
'Make it So' command to his crew in Star Trek).
The software will seek an answer to the question (goal) by interrogating a database containing
Facts and Rules. It does not matter what order the facts and rules are arranged within the
database - unlike procedural languages - the computer will find the best path towards the answer.
There will either be a matching answer - for example the question / goal might be 'who is David's
wife?" or a 'False' is returned where there was no answer to be found.
This type of language is geared more towards applications such as artificial intelligence and
expert systems where inexact data has to be handled or general decisions have to be made.
PROLOG is a declarative language that was developed for artificial intelligence. There are others
such as 'D' also under development.
Example
Consider a Prolog database containing the following facts and rules:
1. Fact: spouse (john, jane)
2. Fact: spouse (david, mary)
3. Fact: spouse(george, susan)
4. Fact: female (jane)
5. Fact: female (mary)
6. Fact: female (susan)
7. Fact: male (john)
8. Fact: male (david)
9. Fact: male (george)
10.
Rule: husband(A,B) IF spouse(A,B) AND male (A)
11.
Rule: wife (A,B) IF spouse(A,B) AND female(B)
A query could then be written:
wife(david,mary)?
The formal name for this statement is 'goal' . And the work of finding the answer is called
'satisfying the goal'.
Effectively this is asking if Mary is the wife of David. In order to provide an answer the
following takes place:
1. Prolog will first of all scan the Rules looking for a match.
2. Rule 11 fits, where A and B can be any two names.
wife (A,B)
3. Then it applies the rule by looking to see if it can find a match to the spouse(A,B)
condition and yes - Fact 2 fits.
CAPE NOTES Unit 1 Module3 Content 11
5
spouse(david,mary)
4. Next it looks to see if the second condition female(B) has a match and yes Fact 5 is a
match.
Fact: female (mary)
5. Both conditions have been met, so yes Mary is David's wife.
Functional Languages
These types of languages are mostly concerned with providing answer to problems purely
through applying calculations to input data.
They do not change anything else other than the input data provided at that point in time. The
technical term is that functional languages have no 'side effects' unlike procedural languages.
These calculations are called 'functions'. Hence the description 'functional language'.
These types of languages are used extensively in highly mathematical areas such as engineering
and financial markets and even network engineering.
Example 1: Mathematica
Mathematica is a powerful functional language to solve mathematical problems.
The following Mathematica statement calculates the root of the equation ex = x2 + 3, starting at
the point x = -2.
In[2] := FindRoot(Exp[x] == x^2 + 3, (x, -2))
Mathematica has a clear syntax with which to write mathematical statements. In this case it is
making use of a built-in function called 'FindRoot' and passing the relevant arguments into it.
Example 2: LISP
Short for 'List Processing'. The first computers were used as pure calculation machines and so
LISP was developed as an efficient way of coding calculations.
LISP is one of the oldest computer languages (1958) and it is still in use today. It specializes in
carrying out calculations and operations across data stored as linked lists. This is called 'list
processing'.
List processing means that the same calculation is carried out over a linked list. The list itself
can be altered during program execution
LISP has been used for a long time in artificial intelligence applications for problems such as
image processing.
Summary
CAPE NOTES Unit 1 Module3 Content 11
6
You have now familiarised yourself with the main programming paradigms and their application
to various types of applications.
Programming Paradigms
Type
Low Level
Procedural
Object
Orientated
Declarative
Functional
Comment
Excellent for producing hardware drivers and other software
requiring a close link to the CPU on which they run such as a linker /
loader / translator
The most common type of programming language, Excellent for
general programming jobs. Languages include 'C', FORTRAN
Another very popular type of computer language. Excellent for
producing re-usable code and for use in large software projects where
data and the methods acting upon that data are kept together.
Languages include C++, Java
Designed for applications such as Artificial Intelligence and Pattern
Recognition applications where deductions need to be made based on
a set of rules. Languages include Prolog
Excellent for programming the computer to act with its original
purpose: calculating. Used for mathematical and simulation
applications. Languages include LISP, Mathematica.
CAPE NOTES Unit 1 Module3 Content 11
7
Download