9.3 Programming Language Paradigms

What is a paradigm? The American Heritage Dictionary of the English Language gives two definitions that relate to how we, in computing, use the term: “one that serves as a pattern or model” and “a set of assumptions, concepts, values, and practices that constitute a way of viewing reality for the community that shares them, especially in an intellectual discipline.”5 In Chapter 1, we outlined the history of software development, listing some of the programming languages that were developed in each generation. Another way to view programming languages is to look at the ways in which different languages reflect differing views of reality—that is, to look at the different paradigms represented.

There are two main paradigms, imperative and declarative, and many subparadigms within each. We later look at different languages within these paradigms.

Imperative Paradigm

The von Neumann model of sequential instructions that operates on values in memory greatly influenced the most common model of a programming language: the imperative model. The dominant languages used in industry throughout the history of computing software come from this paradigm. These languages include FORTRAN, BASIC, C, Pascal, and C++. In this paradigm, the program describes the processing necessary to solve the problem. The imperative paradigm is, therefore, characterized by sequential execution of instructions, the use of variables that represent memory locations, and the use of assignment statements that change the values of these variables.7

Procedural Paradigm

Procedural programming is an imperative model in which the statements are grouped into subprograms. A program is a hierarchy of subprograms, each of which performs a specific task necessary to the solution of the overall program. Our pseudocode examples follow this model. We write subprograms and pass the data to them that they need to accomplish their function.

Object-Oriented Paradigm

The object-oriented view is one of a world of interacting objects. Each object has responsibility for its own actions. In the procedural paradigm, data objects are considered passive and are acted upon by the program. In the object-oriented paradigm, data objects are active. Objects and the code that manipulates them are bundled together, making each object responsible for its own manipulation. SIMULA and Smalltalk were the first two object-oriented programming languages. Java and Python are two modern object-oriented languages.

C++ and Java are imperative languages that are somewhat mixed in terms of their paradigm. Although Java is considered object-oriented, it has some procedural features. C++ is considered procedural, but it has some object-oriented features.

Examples of particular languages are presented in this book, but are not intended to be comprehensive enough to give a student a working knowledge of the language. However, the supplements available for this book include lab exercises for several languages, including Java and C++, which go into more depth.

Declarative Paradigm

The declarative paradigm is a model in which the results are described, but the steps to accomplish the results are not stated. There are two basic models within this paradigm, functional and logic.

Functional Model

The functional model is based on the mathematical concept of the function. Computation is expressed in terms of the evaluation of functions; the solution to a problem is expressed in terms of function calls. Thus the basic mechanism is the evaluation of functions, and there are no variables and no assignment statements. For example, the addition of two values would be expressed this way:

(+ 30 40)

where the parentheses represent an expression to be evaluated by applying the first item (which must be a function) to the rest of the list. This expression is evaluated by applying the addition function to the next two numbers, which returns the value 70. There is no looping construct; repetition is expressed in terms of recursive function calls. The most well-known languages in the functional paradigm are Lisp, Scheme (a derivative of Lisp), and ML.

Let’s examine a series of Scheme expressions that illustrate the flavor of the language. Scheme is interpreted, so the result immediately follows the statement. One interpreter uses #;> as a prompt to enter an expression; we shall use that here.8 The lines without the prompt are what the system returns.

#;> (* 3 4)
12
#;> (+ (* 5 4)(+ 1 4))
25
#;> (length ‘(2 4 6 8 10))
5
#;> (max 2 5 1 3)
5

In the first expression, 3 is multiplied by 4, giving the result 12. In the second expression, the results of multiplying 5 times 4 and adding 1 and 4 are summed, giving 25. The third expression asks for the number of items in the list following the list indicated by the ‘ mark. In the fourth expression, the maximum of the following values is returned.

In Chapter 7, we wrote a recursive algorithm to compute the factorial of a number. lambda is the word for defining a function. Here is the corresponding Scheme code—compare it with the first algorithm:

#;> (define factorial
#;> (lambda(n)
#;> (if
#;> (= n 0)
#;> 1
#;> (* n (factorial (- n 1))))))
#;> (factorial 7)
5040

After the factorial function is defined, giving the name and argument within parentheses executes the function, returning the value 5040.

Logic Programming

Logic programming is based on the principles of symbolic logic. This model comprises a set of facts about objects and a set of rules about the relationships among the objects. A program consists of asking questions about these objects and their relationships, which can be deduced from the facts and the rules. The underlying problem-solving algorithm uses the rules of logic to deduce the answer from the facts and rules.

Prolog is a third-generation logic programming language that was developed in France in 1970. It rose to prominence in 1981, when Japanese researchers announced that logic programming would play a major role in their fifth-generation computer. A Prolog program consists of three types of statements: One type declares facts about objects and their relationships with and to each other; another type defines rules about objects and their relationships; and a third type asks questions about the objects and their relationships.9

For example, the following code defines a set of facts relating pets to owners:

owns(mary,bo).
owns(ann,kitty).
owns(bob,riley).
owns(susy,charlie).

Here owns is the relationship name, the objects are within parentheses, and the period ends the statement of the fact. Does this mean that mary owns bo or bo owns mary? That is up to the programmer. He or she must be consistent in his or her interpretation.

When you have a database of facts, Prolog allows you to ask questions about the database. Look at these three Prolog statements:

?-owns(mary,bo)
?-owns(bo,mary)
?-owns(susy,bo)

The Prolog system replies yes to the first, no to the second, and no to the third.

In Prolog, a constant begins with a lowercase letter and a variable begins with an uppercase letter. We ask questions about facts by substituting a variable for a constant in a fact.

?-owns(ann,Cat).
?-owns(Name,charlie).

In this example, the first statement returns Cat = kitty. The second returns Name = susy.

Both Lisp and Prolog are used in artificial intelligence applications (described in Chapter 13). As you can see, programs in these languages bear little resemblance to the von Neumann architecture reflected in languages in the imperative paradigm as represented in our pseudocode.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.97.61