Download - ProgrammingLanguages Programming Languages Computational Paradigms.

Transcript
Page 1: ProgrammingLanguages Programming Languages Computational Paradigms.

ProgrammingProgramming LanguagesLanguages

Page 2: ProgrammingLanguages Programming Languages Computational Paradigms.

Computational Computational ParadigmsParadigms

Page 3: ProgrammingLanguages Programming Languages Computational Paradigms.

Imperative Programming: Imperative Programming:

• In most cases the computer in question was the In most cases the computer in question was the von Neumann model: a single central processing von Neumann model: a single central processing unit that sequentially executes instructions that unit that sequentially executes instructions that operate on values stored in memory. operate on values stored in memory.

• Programming languages began by imitating Programming languages began by imitating and abstracting the operations of a computer.and abstracting the operations of a computer.

• A programming language that is characterized A programming language that is characterized by these three properties – the sequential by these three properties – the sequential execution of instructions, the use of variables execution of instructions, the use of variables representing memory locations, and the use representing memory locations, and the use of assignment to change the values of of assignment to change the values of variables – is called an imperative language.variables – is called an imperative language.

Page 4: ProgrammingLanguages Programming Languages Computational Paradigms.

The von Neumann BottleneckThe von Neumann Bottleneck

• The requirement that computation be described The requirement that computation be described as a sequence of instructions, each operating on as a sequence of instructions, each operating on a single piece of data, is sometimes referred to a single piece of data, is sometimes referred to as the von Neumann bottleneck.as the von Neumann bottleneck.

• Most programming languages are imperative.Most programming languages are imperative.

• Since it restricts the ability of a language to Since it restricts the ability of a language to indicate parallel computation, and non-indicate parallel computation, and non-deterministic computation, or computation that deterministic computation, or computation that does not depend on order: asynchronousdoes not depend on order: asynchronous

Page 5: ProgrammingLanguages Programming Languages Computational Paradigms.

Functional ProgrammingFunctional Programming

• A functional programming language has as A functional programming language has as its basic mechanism the evaluation of a its basic mechanism the evaluation of a function, or the function call.function, or the function call.

• The functional paradigm bases the description The functional paradigm bases the description of computation on the evaluation of functions or of computation on the evaluation of functions or the application of functions to known values.the application of functions to known values.

• The functional paradigm involves no notion The functional paradigm involves no notion of variable or assignment to variables. of variable or assignment to variables.

• Also, repetitive operations are not expressed Also, repetitive operations are not expressed by loops but by recursive functions.by loops but by recursive functions.

Page 6: ProgrammingLanguages Programming Languages Computational Paradigms.

Functional Programming: Lisp & Functional Programming: Lisp & Ada Ada

• (define (gcd u v) (define (gcd u v) (if (= v 0) u (if (= v 0) u (gcd v (modulo u v))))(gcd v (modulo u v))))

• --------------------------------------------------------------------------------------------------------------------------

• function gcd (u,v: in integer) return integer is function gcd (u,v: in integer) return integer is begin begin if v = 0 then if v = 0 then

return u; return u; else else

return gcd(v,u mod v); return gcd(v,u mod v); end if; end if;

end gcd;end gcd;

Page 7: ProgrammingLanguages Programming Languages Computational Paradigms.

Turing CompleteTuring Complete

• A programming language is Turing complete A programming language is Turing complete if it has integer values, arithmetic functions if it has integer values, arithmetic functions on those values, and if it has a mechanism on those values, and if it has a mechanism for defining new functions using existing for defining new functions using existing functions, selection, and recursion. functions, selection, and recursion.

• The study of recursive function theory in The study of recursive function theory in mathematics has established the following mathematics has established the following property: property:

Page 8: ProgrammingLanguages Programming Languages Computational Paradigms.

Logic ProgrammingLogic Programming

• In a logic programming 1anguage, a In a logic programming 1anguage, a program consists of a set of statements that program consists of a set of statements that describe what is true about a desired result.describe what is true about a desired result.

• This language paradigm is based on This language paradigm is based on symbolic logic.symbolic logic.

• A pure logic programming language has no need A pure logic programming language has no need for control abstractions such as loops or selection.for control abstractions such as loops or selection.

• For this reason, logic programming is also called For this reason, logic programming is also called declarative programming, since properties are declarative programming, since properties are declared, but no execution sequence is specified. declared, but no execution sequence is specified. logic programming languages are referred to as logic programming languages are referred to as very-high-level languages. very-high-level languages.

Page 9: ProgrammingLanguages Programming Languages Computational Paradigms.

Prolog Prolog

gcd(U,V,U) : – V=0. gcd(U,V,U) : – V=0. gcd(U,V,X) : – V > 0, gcd(U,V,X) : – V > 0,

Y is U mod V, Y is U mod V, gcd(V,Y,X).gcd(V,Y,X).

• The gcd of u and v is u if v = 0. The gcd of u and v is u if v = 0.

• The gcd of u and v is the same as the gcd The gcd of u and v is the same as the gcd of v and u mod v if v is > 0. of v and u mod v if v is > 0.

Page 10: ProgrammingLanguages Programming Languages Computational Paradigms.

Object-Oriented Object-Oriented ProgrammingProgramming

• It is based on the notion of an object, which can It is based on the notion of an object, which can be loosely described as a collection of memory be loosely described as a collection of memory locations together with all the operations that can locations together with all the operations that can change the values of these memory locations.change the values of these memory locations.

• Object-oriented programming, not only is a Object-oriented programming, not only is a language paradigm, but also is a language paradigm, but also is a methodology for software program design.methodology for software program design.

• It represents computation as the interaction It represents computation as the interaction among, or communication between, a group among, or communication between, a group of objects, each of which behaves like its of objects, each of which behaves like its own computer, with its own memory and its own computer, with its own memory and its own operations.own operations.

Page 11: ProgrammingLanguages Programming Languages Computational Paradigms.

Object ClassesObject Classes

• Classes are defined using declarations, much as Classes are defined using declarations, much as structured types are declared in a language like Cstructured types are declared in a language like C

• In many object-oriented languages, objects In many object-oriented languages, objects are grouped into classes that represent all are grouped into classes that represent all the objects with the same properties.the objects with the same properties.

• Objects are then created as particular Objects are then created as particular examples, or instances, of a class. examples, or instances, of a class.

Page 12: ProgrammingLanguages Programming Languages Computational Paradigms.

Example in JavaExample in Java

public class IntWithGcd; public class IntWithGcd; { public IntWithGcd(int val) { value { public IntWithGcd(int val) { value

= val;} = val;} public int intValue() { return public int intValue() { return value;} value;} public int gcd ( int v) public int gcd ( int v)

{ int z = value; { int z = value; int y = int y =

v; v; while while (y != 0) (y != 0) { int t { int t = x; = x; y = y = z % y; z % y; z = z = t; t; }} return z; return z;

} } private int private int value; } value; }

Page 13: ProgrammingLanguages Programming Languages Computational Paradigms.

Example in JavaExample in Java

• This class can be used by defining an This class can be used by defining an object of the class as follows: object of the class as follows:

IntWithGcd x; IntWithGcd x;

• Then we can ask x to tell us its value by Then we can ask x to tell us its value by calling the value procedure, as for calling the value procedure, as for example, in example, in int y = x.gcd(18); int y = x.gcd(18);

• we must create, or instantiate, the object we must create, or instantiate, the object with the following with the following statement: statement:

x = new IntWithGcd(8); x = new IntWithGcd(8);

Page 14: ProgrammingLanguages Programming Languages Computational Paradigms.

Event-Driven Visual Event-Driven Visual ProgrammingProgramming

• All the paradigms we’ve examined are based All the paradigms we’ve examined are based on a fundamental model of computation in on a fundamental model of computation in which the program design predetermines what which the program design predetermines what will occur when the program is run. will occur when the program is run.

• They are written to run reasonably to any They are written to run reasonably to any particular sequence of events that may particular sequence of events that may occur once execution begins,occur once execution begins,

• Event-driven programs do not predict the Event-driven programs do not predict the control sequence that will occur;control sequence that will occur;

• In this model, the input data govern the In this model, the input data govern the particular sequence of control that is particular sequence of control that is actually carried out by the program.actually carried out by the program.

Page 15: ProgrammingLanguages Programming Languages Computational Paradigms.

Event-Driven Visual Event-Driven Visual ProgrammingProgramming

• Execution of an event-driven program does Execution of an event-driven program does not typically terminate; such a program is not typically terminate; such a program is designed to run for an arbitrary period of designed to run for an arbitrary period of time, often indefinitely. time, often indefinitely.

• The most widespread example of an event-The most widespread example of an event-driven program is the GUI mouse- and, driven program is the GUI mouse- and, windows-driven user interface.windows-driven user interface.

• Event-driven programs also drive web-based Event-driven programs also drive web-based applications.applications.

Page 16: ProgrammingLanguages Programming Languages Computational Paradigms.

Event-Driven Visual Event-Driven Visual ProgrammingProgramming

• To provide effective support for event-driven To provide effective support for event-driven programming, some languages have developed programming, some languages have developed some basic terminology and principles of design. some basic terminology and principles of design.

• Most recently, these principles have appeared in Most recently, these principles have appeared in Java, though other languages like Visual Basic Java, though other languages like Visual Basic also support event-driven programming. also support event-driven programming.