Programming Paradigms Question: Given Programming Languages Naturally Fall Into a Number of a = a + 1; Fundamental Styles Or If (A > 10) Paradigms
Total Page:16
File Type:pdf, Size:1020Kb
Programming Paradigms Question: Given Programming languages naturally fall into a number of a = a + 1; fundamental styles or if (a > 10) paradigms. b = 10; else b = 15; a = a * b; Procedural Languages Most of the widely-known and Why can’t 5 processors each widely-used programming execute one line to make the languages (C, Fortran, Pascal, program run 5 times faster? Ada, etc.) are procedural. Programs execute statement by statement, reading and modifying a shared memory. This programming style closely models conventional sequential processors linked to a random access memory (RAM). © © CS 538 Spring 2007 35 CS 538 Spring 2007 36 Functional Languages Object-Oriented Languages Lisp, Scheme and ML are C++, Java, Smalltalk, Pizza and functional in nature. Python are object-oriented. Programs are expressions to be Data and functions are evaluated. encapsulated into Objects. Language design aims to Objects are active, have minimize side-effects, including persistent state, and uniform assignment. interfaces (messages or Alternative evaluation methods). mechanisms are possible, Notions of inheritance and including common interfaces are central. Lazy (Demand Driven) All objects that provide the same interface are treated uniformly. In Java you can print Eager (Data Driven or any object that provides the Speculative) toString method. Iteration through the elements of any object that implements the Enumeration interface is possible. © © CS 538 Spring 2007 37 CS 538 Spring 2007 38 Subclassing allows to you Logic Programming extend or redefine part of an Languages object’s behavior without Prolog notes that most reprogramming all of the programming languages object’s definition. Thus in Java, address both the logic of a you can take a Hashtable class program (what is to be done) (which is fairly elaborate) and and its control flow (how you create a subclass in which an do what you want). existing method (like toString) is redefined, or new operations A logic programming language, are added. like Prolog, lets programmers focus on a program’s logic without concern for control issue. These languages have no real control structures, and little notion of “flow of control.” What results are programs that are unusually succinct and focused. © © CS 538 Spring 2007 39 CS 538 Spring 2007 40 Example: Review of Concepts from inOrder( [] ). Procedural Programming inOrder( [ _ ] ). inOrder([a,b|c]) :- (a<b), Languages inOrder([b|c]). Declarations/Scope/Lifetime/ This is a complete, executable Binding function that determines if a list is in order. It is naturally Static/Dynamic polymorphic, and is not • Identifiers are declared, either cluttered with declarations, explicitly or implicitly (from context of variables or explicit loops. first use). • Declarations bind type and kind information to an identifier. Kind specifies the grouping of an identifier (variable, label, function, type name, etc.) • Each identifier has a scope (or range) in a program—that part of the program in which the identifier is visible (i.e., may be used). © © CS 538 Spring 2007 41 CS 538 Spring 2007 42 • Data objects have a lifetime—the span Properties of an identifier (and the of time, during program execution, object it represents) may be set at during which the object exists and • Compile-time may be used. • Lifetimes of data objects are often tied These are static properties as to the scope of the identifier that they do not change during denotes them. The objects are created execution. Examples include when its identifier’s scope is entered, the type of a variable, the value and they may be deleted when the of a constant, the initial value identifier’s scope is exited. For of a variable, or the body of a example, memory for local variables function. within a function is usually allocated when the function is called (activated) • Run-time and released when the call terminates. These are dynamic properties. In Java, a method may be loaded into Examples include the value of a memory when the object it is a variable, the lifetime of a heap member of is first accessed. object, the value of a function’s parameter, the number of times a while loop iterates, etc. © © CS 538 Spring 2007 43 CS 538 Spring 2007 44 Example: Block Structured Languages In Fortran • Include Algol 60, Pascal, C and Java. • The scope of an identifier is the whole • Identifiers may have a non-global program or subprogram. scope. Declarations may be local to a class, subprogram or block. • Each identifier may be declared only once. • Scopes may nest, with declarations propagating to inner (contained) • Variable declarations may be implicit. scopes. (Using an identifier implicitly declares it as a variable.) • The lexically nearest declaration of an identifier is bound to uses of that • The lifetime of data objects is the whole program. identifier. © © CS 538 Spring 2007 45 CS 538 Spring 2007 46 Binding of an identifier to its Block Structure Concepts corresponding declaration is usually static (also called • Nested Visibility lexical), though dynamic No access to identifiers outside binding is also possible. their scope. Static binding is done prior to • Nearest Declaration Applies execution—at compile-time. Static name scoping. Example (drawn from C): • Automatic Allocation and Deallocation of Locals int x,z; Lifetime of data objects is void A() { bound to the scope of the float x,y; print(x,y,z); Identifiers that denote them. int } float void B() { float print (x,y,z) int } undeclared int © © CS 538 Spring 2007 47 CS 538 Spring 2007 48 Variations in these rules of Dynamic Scoping name scoping are possible. An alternative to static scoping For example, in Java, the is dynamic scoping, which was lifetime of all class objects is used in early Lisp dialects (but from the time of their creation not in Scheme, which is (via new) to the last visible reference to them. statically scoped). Thus Under dynamic scoping, identifiers are bound to the ... Object O;... dynamically closest declaration creates an object reference but of the identifier. Thus if an does not allocate any memory identifier is not locally space for O. declared, the call chain You need (sequence of callers) is examined to find a matching ... Object O = new Object(); ... declaration. to actually create memory space for O. © © CS 538 Spring 2007 49 CS 538 Spring 2007 50 Example: Dynamic scoping makes type checking and variable access int x; harder and more costly than void print() { static scoping. (Why?) write(x); } However, dynamic scoping main () { does allow a notion of an bool x; “extended scope” in which print(); declarations extend to } subprograms called within that Under static scoping the x scope. written in print is the lexically Though dynamic scoping may closest declaration of x, which seen a bit bizarre, it is closely is as an int. related to virtual functions Under dynamic scoping, since used in C++ and Java. print has no local declaration of x, print’s caller is examined. Since main calls print, and it has a declaration of x as a bool, that declaration is used. © © CS 538 Spring 2007 51 CS 538 Spring 2007 52.