L5Pdm - Programming Paradigms s Procedural/Imperative s...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Programming Paradigms s Procedural/Imperative s Functional s Logic s Object-Oriented cs784(Prasad) L5Pdm 1 Imperative vs Non-Imperative Functional/Logic programs specify WHAT is to be computed abstractly, leaving the details of data organization and instruction sequencing to the interpreter. s In constrast, Imperative programs describe the details of HOW the results are to be obtained, in terms of the underlying machine model. s cs784(Prasad) L5Pdm 2 Illustrative Example Expression (to be computed) : a + b + c s Recipe for Computation: s – Intermediate Code » T := a + b; T := T + c; – Accumulator Machine » Load a; Add b; Add c – Stack Machine » Push a; Push b; Add; Push c; Add cs784(Prasad) L5Pdm 3 Imperative vs Non-Imperative Functional/Logic style clearly separates WHAT aspects of a program (programmers’ responsibility) from the HOW aspects (implementation decisions). s An Imperative program contains both the specification and the implementation details, inseparably inter-twined. s cs784(Prasad) L5Pdm 4 Procedural vs Functional s s s s Program: a sequence of instructions for a von Neumann m/c. Computation by instruction execution. Iteration. Modifiable or updateable variables. s s s s Program: a collection of function definitions (m/c independent). Computation by term rewriting. Recursion. Assign-only-once variables. 5 cs784(Prasad) L5Pdm Functional Style : Illustration Definition : Equations sum(0) = 0 sum(n) = n + sum(n-1) s Computation : Substituition and Replacement sum(2) = 2 + sum (2-1) = … = 3 s cs784(Prasad) L5Pdm 6 Paradigm vs Language s Imperative Style i := 0; sum := 0; while (i < n) do i := i + 1; sum := sum + i end; – Storage efficient s Functional Style func sum(n:int) : int; if n = 0 then 0 else n + sum(n-1) end; – No Side-effect cs784(Prasad) L5Pdm 7 Role of Variables Imperative (read/write) i 0 1 2 3 ... sum 0 1 3 6 ... s Functional (read only) n1 6 3 n2 2 3 n3 1 1 cs784(Prasad) ... L5Pdm s sum1 sum2 sum3 8 Bridging the Gap s Tail recursive programs can be auomatically optimized for space by translating them into equivalent whileloops. func sum(n : int, r : int) : int; if n = 0 then r else sum(n-1, n+r) end – Scheme does not have loops. L5Pdm 9 cs784(Prasad) Analogy: Styles vs Formalisms s Iteration Tail-Recursion General Recursion s Regular Expression Regular Grammar Context-free Grammar s s s s cs784(Prasad) L5Pdm 10 Logic Programming Paradigm s Integrates Data and Control Structures edge(a,b). edge(a,c). edge(c,a). path(X,X). path(X,Y) :- edge(X,Y). path(X,Y) :- edge(X,Z), path(Z,Y). L5Pdm 11 cs784(Prasad) Declarative Programming A logic program defines a set of relations. This “knowledge” can be used in various ways by the interpreter to solve different queries. s In contrast, the programs in other languages make explicit HOW the “declarative knowledge” is used to solve the query. s cs784(Prasad) L5Pdm 12 Append in Prolog append(, L, L). append([ H | T ], L, [ H | R ]) :append(T, L, R). True statements about append relation. » “.” and “:-” are logical connectives that stand for “and” and “if” respectively. s s Uses pattern matching. » “” and “|” stand for empty list and cons operation. L5Pdm 13 cs784(Prasad) Different Kinds of Queries Verification – sig: list x list x list » append([1], [2,3], [1,2,3]). s s Concatenation – sig: list x list -> list » append([1], [2,3], R). cs784(Prasad) L5Pdm 14 More Queries s Constraint solving – sig: list x list -> list » append( R, [2,3], [1,2,3]). – sig: list -> list x list » append(A, B, [1,2,3]). s Generation – sig: -> list x list x list » append(X, Y, Z). cs784(Prasad) L5Pdm 15 Object-Oriented Style s Programming with Abstract Data Types – ADTs specify/describe behaviors. s Basic Program Unit: Class – Implementation of an ADT. » Abstraction enforced by encapsulation.. s Basic Run-time Unit: Object – Instance of a class. » Has an associated state. cs784(Prasad) L5Pdm 16 Procedural vs Object-Oriented s s s Emphasis on procedural abstraction. Top-down design; Step-wise refinement. Suited for programming in the small. s s s Emphasis on data abstraction. Bottom-up design; Reusable libraries. Suited for programming in the large. cs784(Prasad) L5Pdm 17 Integrating Heterogeneous Data s In C, Pascal, etc., use Union Type / Switch Statement Variant Record Type / Case Statement In C++, Java, Eiffel, etc., use Abstract Classes / Virtual Functions Interfaces and Classes / Dynamic Binding L5Pdm 18 s cs784(Prasad) Comparison : Figures example s Data – Square » side s Classes – Square » side » area (= side * side) – Circle » radius s Operation (area) – Square » side * side – Circle » radius » area (= PI*radius*radius) – Circle » PI * radius * radius cs784(Prasad) L5Pdm 19 Adding a new operation s s s Data ... Operation (area) Operation (perimeter) – Square » 4 * side s Classes – Square » ... » perimeter (= 4 * side) – Circle » ... » perimeter (= 2 * PI * radius) – Circle » 2 * PI * radius cs784(Prasad) L5Pdm 20 Adding a new data representation s Data – ... – rectangle » length » width s Classes – ... – rectangle » » » length width area (= length * width) s Operation (area) – ... – rectangle » length * width cs784(Prasad) L5Pdm 21 Procedural vs Object-Oriented New operations cause additive changes in procedural style, but require modifications to all existing “class modules” in objectoriented style. s New data representations cause additive changes in object-oriented style, but require modifications to all “procedure modules”. s cs784(Prasad) L5Pdm 22 Object-Oriented Concepts Data Abstraction (specifies behavior) s Encapsulation (controls visibility of names) s Polymorphism (accommodates various implementations) s Inheritance (facilitates code reuse) s Modularity (relates to unit of compilation) s cs784(Prasad) L5Pdm 23 Example : Role of interface in decoupling s Client » Determine the number of elements in a collection. s Suppliers » Collections : Vector, String, List, Set, Array, etc s Procedual Style » A client is responsible for invoking appropriate supplier function for determining the size. s OOP Style » Suppliers are responsible for conforming to the standard interface required for exporting the size functionality to a client. cs784(Prasad) L5Pdm 24 Client in Scheme (define (size C) (cond ( (vector? C) (vector­length C) ) ( (pair? C) (length C) ) ( (string? C) (string­length C) ) ( else “size not supported”) ) )) (size (vector 1 2 (+ 1 2))) (size ‘(one “two” 3)) cs784(Prasad) L5Pdm 25 Suppliers and Client in Java interface Collection { int size(); } class myVector extends Vector implements Collection { } class myString extends String implements Collection { public int size() { return length();} } class myArray implements Collection { int array; public int size() {return array.length;} } Collection c = new myVector(); c.size(); cs784(Prasad) L5Pdm 26 ...
View Full Document

This note was uploaded on 06/02/2011 for the course CS cs784 taught by Professor Tkprasad during the Spring '11 term at New York Institute of Technology-Westbury.

Ask a homework question - tutors are online