Programming Paradigms Procedural Functional Logic Object-Oriented.

Slides:



Advertisements
Similar presentations
Agenda Definitions Evolution of Programming Languages and Personal Computers The C Language.
Advertisements

Programming Languages and Paradigms
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Introduction to Programming Languages Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
High-Level Programming Languages
Stéphane Ducasse6.1 Essential Concepts Why OO? What is OO? What are the benefits? What are the KEY concepts? Basis for all the lectures.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Chapter 8 High-Level Programming Languages Nell Dale John Lewis.
ISBN Lecture 01 Preliminaries. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Lecture 01 Topics Motivation Programming.
CSE S. Tanimoto Syntax and Types 1 Representation, Syntax, Paradigms, Types Representation Formal Syntax Paradigms Data Types Type Inference.
Programming Paradigms cs784(Prasad)L5Pdm1. Programming Paradigm A way of conceptualizing what it means to perform computation and how tasks to be carried.
CEG860 (Prasad)L3EG1 Object-Oriented Programming Examples.
Chapter 10 Classes Continued
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
C++ fundamentals.
Overview. Copyright © 2006 The McGraw-Hill Companies, Inc. Chapter 1 Overview A good programming language is a conceptual universe for thinking about.
CSE 341, S. Tanimoto Concepts 1- 1 Programming Language Concepts Formal Syntax Paradigms Data Types Polymorphism.
Programming Languages and Paradigms Object-Oriented Programming.
Imperative Programming
Welcome to OBJECT ORIENTED PROGRAMMIN Date: 10/09/2014 Prepared By Prepared By : VINAY ALEXANDER PGT(CS) KV jhagrakhand.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
CS 363 Comparative Programming Languages
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
By Neng-Fa Zhou1 Evolution of programming languages –Machine language –Assembly language –Sub-routines and loop (Fortran) –Procedures and recursion (Algol,
Software Architecture & Complexity By:- Pardesi Band Group
1 Programming Language History and Evolution In Text: Chapter 2.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
1 CSC 222: Computer Programming II Spring 2004 See online syllabus at: Course goals:
Chapter 6 Programming Languages © 2007 Pearson Addison-Wesley. All rights reserved.
CS112: Structure of Programming Languages A smorgasbord of topics in programming languages including programming paradigms and syntax, semantics, implementation.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Programming Languages and Paradigms Imperative Programming.
Prasad1 Toward a Comprehensive Supplement for Language Courses Krishnaprasad Thirunarayan & Stephen P. Carl Department of Computer Science & Engineering.
Theory of Programming Languages Introduction. What is a Programming Language? John von Neumann (1940’s) –Stored program concept –CPU actions determined.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Chapter 8 High-Level Programming Languages. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Salman Marvasti Sharif University of Technology Winter 2015.
Chapter 4 Software. Chapter 4: Software Generations of Languages Each computer is wired to perform certain operations in response to an instruction. An.
S.Ducasse Stéphane Ducasse 1 Essential OO Concepts Stéphane Ducasse.
ISBN Object-Oriented Programming Chapter Chapter
Cs774 (Prasad)L1LP1 Programming Paradigms Logic Programming Paradigm Correctness > Efficiency
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 1 Overview A good programming language is.
Introduction to OOP CPS235: Introduction.
LECTURE 18 Control Flow. CONTROL FLOW Sequencing: the execution of statements and evaluation of expressions is usually in the order in which they appear.
Introduction to Objects and Encapsulation Computer Science 4 Mr. Gerb Reference: Objective: Understand Encapsulation and abstract data types.
Lecture 2 Intro. To Software Engineering and Object-Oriented Programming (2/2)
Welcome to OBJECT ORIENTED PROGRAMMING Prepared By Prepared By : VINAY ALEXANDER PGT(CS) KV jhagrakhand.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Language Paradigms CS655.
Programming Language History and Evolution
Programming Languages 2nd edition Tucker and Noonan
Functional Programming
Programming Language Syntax and semantics
Visit for more Learning Resources
Programming Paradigms
PROGRAMMING LANGUAGES
Types of Programming Languages
Evolution of programming languages
Procedural vs Functional Style
Programming Languages 2nd edition Tucker and Noonan
Overview of Programming Paradigms
Programming Paradigms
Presentation transcript:

Programming Paradigms Procedural Functional Logic Object-Oriented

Specifying the WHAT Describe the Inputs – Specific values – Properties Describe the Outputs (as above) Describe the Relationships Between I x O – As a possibly infinite table – Equations and other predicates between input and output expressions – For a given input, output may not be unique

Specifying the HOW Describe the Inputs – Specific values – Properties Describe HOW the Outputs are produced Models of existing computers – Program State – Control Flow A Few Abstractions – Block Structure – Recursion via a Stack

Procedural programming Describes the details of HOW the results are to be obtained, in terms of the underlying machine model. Describes computation in terms of – Statements that change a program state – Explicit control flow Synonyms – Imperative programming – Operational Fortran, C, … – Abstractions of typical machines – Control Flow Encapsulation Control Structures Procedures – No return values Functions – Return one or more values Recursion via stack

Procedural Programming: State Program State – Collection of Variables and their values – Contents of variables change Expressions – Not expected to change Program State Assignment Statements Other Statements Side Effects

C, C++, C#, Java Abstractions of typical machines Control Flow Encapsulation – Control Structures – Procedures No return values – Functions Return one or more values – Recursion via stack Better Data Type support

Illustrative Example Expression (to be computed) : a + b + c Recipe for Computation – Account for machine limitations – Intermediate Location T := a + b; T := T + c; – Accumulator Machine Load a; Add b; Add c – Stack Machine Push a; Push b; Add; Push c; Add

Declarative Programming Specifies WHAT is to be computed abstractly Expresses the logic of a computation without describing its control flow Declarative languages include – logic programming, and – functional programming. often defined as any style of programming that is not imperative.

Imperative vs Non-Imperative Functional/Logic style clearly separates WHAT aspects of a program (programmers’ responsibility) from the HOW aspects (implementation decisions). An Imperative program contains both the specification and the implementation details, inseparably inter-twined.

Procedural vs Functional Program: a sequence of instructions for a von Neumann m/c. Computation by instruction execution. Iteration. Modifiable or updatable variables.. Program: a collection of function definitions (m/c independent). Computation by term rewriting. Recursion. Assign-only-once variables.

Functional Style : Illustration Definition: Equations sumto(0) = 0 sumto(n) = n + sumto(n-1) Computation: Substitution and Replacement sumto(2)= 2 + sumto (2-1) = 2 + sumto(1) = sumto(1-1) = sumto(0) = = … = 3

Paradigm vs Language Imperative Style tsum := 0; i := 0; while (i < n) do i := i + 1; tsum := tsum + I od Storage efficient Functional Style func sumto(n: int): int; if n = 0 then0 elsen + sumto(n-1) fi endfunc; No Side-effect

Bridging the Gap Imperative is not always faster, or more memory efficient than functional. E.g., tail recursive programs can be automatically translated into equivalent while-loops. func xyz(n : int, r : int) : int; if n = 0 then r else xyz(n-1, n+r) fi endfunc

Analogy: Styles vs Formalisms Iteration Tail-Recursion General Recursion Regular Expression Regular Grammar Context-free Grammar

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).

Logic Programming A logic program defines a set of relations. This “knowledge” can be used in various ways by the interpreter to solve different “queries”. In contrast, the programs in other languages Make explicit HOW the “declarative knowledge” is used to solve the query.

Append in Prolog append([], L, L). append([ H | T ], X, [ H | Y ]) :- append(T, X, Y). True statements about append relation. Uses pattern matching. – “[]” and “|” stand for empty list and cons operation.

Different Kinds of Queries Verification – append: list x list x list append([1], [2,3], [1,2,3]). Concatenation – append: list x list -> list append([1], [2,3], R).

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]). Generation – append: -> list x list x list append(X, Y, Z).

Object-Oriented Style Programming with Abstract Data Types – ADTs specify/describe behaviors. Basic Program Unit: Class – Implementation of an ADT. Abstraction enforced by encapsulation.. Basic Run-time Unit: Object – Instance of a class. Has an associated state.

Procedural vs Object-Oriented Emphasis on procedural abstraction. Top-down design; Step- wise refinement. Suited for programming in the small. Emphasis on data abstraction. Bottom-up design; Reusable libraries. Suited for programming in the large.

Integrating Heterogeneous Data 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

Comparison : Figures example Data – Square side – Circle radius Operation (area) – Square side * side – Circle PI * radius * radius Classes – Square side area (= side * side) – Circle radius area (= PI*radius*radius)

Adding a new operation Data... Operation (area) Operation (perimeter) – Square 4 * side – Circle 2 * PI * radius Classes – Square... perimeter (= 4 * side) – Circle... perimeter (= 2 * PI * radius)

Adding a new data representation Data –... – rectangle length width Operation (area) –... – rectangle length * width Classes –... – rectangle length width area (= length * width)

Procedural vs Object-Oriented New operations cause additive changes in procedural style, but require modifications to all existing “class modules” in object-oriented style. New data representations cause additive changes in object-oriented style, but require modifications to all “procedure modules”.

Object-Oriented Concepts Data Abstraction (specifies behavior) Encapsulation (controls visibility of names) Polymorphism (accommodates various implementations) Inheritance (facilitates code reuse) Modularity (relates to unit of compilation)

Example : Role of interface in decoupling Client – Determine the number of elements in a collection. Suppliers – Collections : Vector, String, List, Set, Array, etc Procedural Style – A client is responsible for invoking appropriate supplier function for determining the size. OOP Style – Suppliers are responsible for conforming to the standard interface required for exporting the size functionality to a client.

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))

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();