programming paradigms
play

Programming Paradigms Procedural Functjonal Logic Object-Oriented - PowerPoint PPT Presentation

Programming Paradigms Procedural Functjonal Logic Object-Oriented Specifying the WHAT Describe the Inputs Specifjc values Propertjes Describe the Outputs (as above) Describe the Relatjonships Between I x O As


  1. Programming Paradigms • Procedural • Functjonal • Logic • Object-Oriented

  2. Specifying the WHAT • Describe the Inputs – Specifjc values – Propertjes • Describe the Outputs (as above) • Describe the Relatjonships Between I x O – As a possibly infjnite table – Equatjons and other predicates between input and output expressions – For a given input, output may not be unique CS784 2

  3. Specifying the HOW • Describe the Inputs – Specifjc values – Propertjes • Describe HOW the Outputs are produced • Models of existjng computers – Program State – Control Flow • A Few Abstractjons – Block Structure – Recursion via a Stack CS784 3

  4. Procedural programming • Describes the details of HOW the results are to be obtained, in terms of the underlying machine model. • Describes computatjon in terms of – Statements that change a program state – Explicit control fmow • Synonyms – Imperatjve programming – Operatjonal • Fortran, C, … – Abstractjons of typical machines – Control Flow Encapsulatjon • Control Structures • Procedures – No return values • Functjons – Return one or more values • Recursion via stack CS784 4

  5. Procedural Programming: State • Program State – Collectjon of Variables and their values – Contents of variables change • Expressions – Not expected to change Program State • Assignment Statements • Other Statements • Side Efgects CS784 5

  6. C, C++, C#, Java • Abstractjons of typical machines • Control Flow Encapsulatjon – Control Structures – Procedures • No return values – Functjons • Return one or more values – Recursion via stack • Betuer Data Type support CS784 6

  7. Illustratjve Example • Expression (to be computed) : a + b + c • Recipe for Computatjon – Account for machine limitatjons – Intermediate Locatjon • 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 7

  8. Declaratjve Programming • Specifjes WHAT is to be computed abstractly • Expresses the logic of a computatjon without describing its control fmow • Declaratjve languages include – logic programming, and – functjonal programming. • ofuen defjned as any style of programming that is not imperatjve. CS784 8

  9. Imperatjve vs Non-Imperatjve • Functjonal/Logic style clearly separates WHAT aspects of a program (programmers’ responsibility) from the HOW aspects (implementatjon decisions). • An Imperatjve program contains both the specifjcatjon and the implementatjon details, inseparably inter-twined. CS784 9

  10. Procedural vs Functjonal • Program: a sequence of • Program: a collectjon of instructjons for a von functjon defjnitjons Neumann m/c. (m/c independent). • Computatjon by • Computatjon by term instructjon executjon. rewritjng. • Iteratjon. • Recursion. • Modifjable or updatable • Assign-only-once variables.. variables. CS784 10

  11. Functjonal Style : Illustratjon • Defjnitjon: Equatjons sumto(0) = 0 sumto(n) = n + sumto(n-1) • Computatjon: Substjtutjon and Replacement sumto(2) = 2 + sumto (2-1) = 2 + sumto(1) = 2 + 1 + sumto(1-1) = 2 + 1 + sumto(0) = 2 + 1 + 0 = … = 3 CS784 11

  12. Paradigm vs Language Imperatjve Style Functjonal Style tsum := 0; func sumto(n: int): int; i := 0; if n = 0 while (i < n) do then 0 i := i + 1; else n + sumto(n-1) tsum := tsum + I fj od endfunc; No Side-efgect Storage effjcient CS784 12

  13. Bridging the Gap • Imperatjve is not always faster, or more memory effjcient than functjonal. • E.g., tail recursive programs can be automatjcally translated into equivalent while-loops. func xyz(n : int, r : int) : int; if n = 0 then r else xyz(n-1, n+r) fj endfunc CS784 13

  14. Analogy: Styles vs Formalisms • Iteratjon • Regular Expression • Tail-Recursion • Regular Grammar • General Recursion • Context-free Grammar CS784 14

  15. Logic Programming Paradigm 1. edge(a,b). 2. edge(a,c). 3. edge(c,a). 4. path(X,X). 5. path(X,Y) :- edge(X,Y). 6. path(X,Y) :- edge(X,Z), path(Z,Y). CS784 15

  16. Logic Programming • A logic program defjnes a set of relatjons. • This “knowledge” can be used in various ways by the interpreter to solve difgerent “queries”. • In contrast, the programs in other languages • Make explicit HOW the “declaratjve knowledge” is used to solve the query. CS784 16

  17. Append in Prolog • append([], L, L). • append([ H | T ], X, [ H | Y ]) :- • append(T, X, Y). • True statements about append relatjon. • Uses patuern matching. – “[]” and “|” stand for empty list and cons operatjon. CS784 17

  18. Difgerent Kinds of Queries • Verifjcatjon – append: list x list x list • append([1], [2,3], [1,2,3]). • Concatenatjon – append: list x list -> list • append([1], [2,3], R). CS784 18

  19. More Queries • Constraint solving – append: list x list -> list • append( R, [2,3], [1,2,3]). – append: list -> list x list • append(A, B, [1,2,3]). • Generatjon – append: -> list x list x list • append(X, Y, Z). CS784 19

  20. Object-Oriented Style • Programming with Abstract Data Types – ADTs specify/describe behaviors. • Basic Program Unit: Class – Implementatjon of an ADT. • Abstractjon enforced by encapsulatjon.. • Basic Run-tjme Unit: Object – Instance of a class. • Has an associated state. CS784 20

  21. Procedural vs Object-Oriented • Emphasis on procedural • Emphasis on data abstractjon. abstractjon. • Top-down design; Step- • Botuom-up design; wise refjnement. Reusable libraries. • Suited for programming • Suited for programming in the small. in the large. CS784 21

  22. Integratjng Heterogeneous Data • In C, Pascal, etc., use • Union Type / Switch Statement • Variant Record Type / Case Statement • In C++, Java, Eifgel, etc., use • Abstract Classes / Virtual Functjons • Interfaces and Classes / Dynamic Binding CS784 22

  23. Comparison : Figures example • Data • Classes – Square – Square • side • side • area – Circle • (= side * side) • radius – Circle • Operatjon (area) • radius – Square • area • side * side • (= PI*radius*radius) – Circle • PI * radius * radius CS784 23

  24. Adding a new operatjon • Data • Classes – Square • ... • ... • Operatjon (area) • perimeter • Operatjon (perimeter) • (= 4 * side) – Square – Circle • 4 * side • ... • perimeter – Circle • (= 2 * PI * radius) • 2 * PI * radius CS784 24

  25. Adding a new data representatjon • Data • Classes – ... – ... – rectangle – rectangle • length • length • width • width • area • Operatjon (area) • (= length * width) – ... – rectangle • length * width CS784 25

  26. Procedural vs Object-Oriented • New operatjons cause additjve changes in procedural style, but require modifjcatjons to all existjng “class modules” in object-oriented style. • New data representatjons cause additjve changes in object-oriented style, but require modifjcatjons to all “procedure modules”. CS784 26

  27. Object-Oriented Concepts • Data Abstractjon (specifjes behavior) • Encapsulatjon (controls visibility of names) • Polymorphism (accommodates various implementatjons) • Inheritance (facilitates code reuse) • Modularity (relates to unit of compilatjon) CS784 27

  28. Example : Role of interface in decoupling • Client – Determine the number of elements in a collectjon. • Suppliers – Collectjons : Vector, String, List, Set, Array, etc • Procedural Style – A client is responsible for invoking appropriate supplier functjon for determining the size. • OOP Style – Suppliers are responsible for conforming to the standard interface required for exportjng the size functjonality to a client. CS784 28

  29. Client in Scheme • (defjne (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 29

  30. Suppliers and Client in Java Interface Collectjon {int size(); } class myVector extends Vector implements Collectjon { } class myString extends String implements Collectjon { public int size() { return length();} } class myArray implements Collectjon { int[] array; public int size() {return array.length;} } Collectjon c = new myVector(); c.size(); CS784 30

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend