Lecture on Programming Languages Chapter#5
Programming Paradigms/Models Paradigm defines a model or way of programming We will discuss the following three paradigms Imperative/Procedural Paradigm Declarative Paradigm Object Oriented Paradigm
The evolution of programming paradigms
The composition of a typical imperative program or program unit
Procedural Units Local versus Global Variables Formal versus Actual Parameters Passing parameters by value versus reference Procedures versus Functions
The flow of control involving a procedure
Example of procedure written in C language
Executing the procedure Demo and passing parameters by value
Executing the procedure Demo and passing parameters by reference
Object Oriented Paradigm In this approach all real world things are considered as an object. Each object has a certain set of qualities/ attributes and each object can perform some job/method. Thus if I have an object CAR then Car has its color, engine#, make, model etc as attributes and its methods can be drive(), stop() etc Examples include C++, Java etc
Declarative Programming Approach It emphasizes the question “What is the problem?” rather than “What algorithm is required for solving the problem?” Here a general problem solving approach is developed that can solve a number of problems. These languages are difficult to design and are special purpose by nature PROLOG (Programming LOGic) is an example
Figure 6.25 Resolving the statements (P OR Q), (R OR ¬Q), ¬R, and ¬P
Declarative Programming Resolution: Combining two or more statements to produce a new statement (that is a logical consequence of the originals). Example: (P OR Q) AND (R OR Q) resolves to (P OR R) Resolvent: A new statement deduced by resolution Clause form: A statement whose elementary components are connected by the Boolean operation OR Unification: Assigning a value to a variable so that two statements become “compatible.”
Prolog Fact: A Prolog statement establishing a fact Consists of a single predicate Form: predicateName(arguments). Example: parent(bill, mary). Rule: A Prolog statement establishing a general rule Form: conclusion :- premise. :- means “if” Example: wise(X) :- old(X). Example: faster(X,Z) :- faster(X,Y), faster(Y,Z).