
Programming Paradigms During the next few weeks we are going to work with CSc 520 functional programming. Before I can explain to you what FP is, I thought I’d better put things into Principles of Programming perspective by talking about other Languages programming paradigms. Over the last 40 or so years, a number of programming 2: Functional Programming paradigms (a programming paradigm is a way to think Christian Collberg about programs and programming) have emerged. [email protected] Department of Computer Science University of Arizona Copyright c 2004 Christian Collberg 520—Spring 2005—2 [1] 520—Spring 2005—2 [2] Programming Paradigms. Programming Paradigms. A programming paradigm Imperative Programming is a way to think about programs, programming, and Programming with state. problem solving, Also known as procedural programming. The first to is supported by one or more programming languages. emerge in the 1940s-50s. Still the way most people learn how to program. Being familiar with several paradigms makes you a better programmer and problem solver. The most popular FORTRAN, Pascal, C, BASIC. paradigms: Functional Programming 1. Imperative programming. 2. Functional programming. Programming with values. 3. Object-oriented programming. Arrived in the late 50s with the LISP language. LISP is 4. Logic Programming. still popular and widely used by AI people. When all you have is a hammer, everything looks like a nail. LISP, Miranda, Haskell, Gofer. 520—Spring 2005—2 [3] 520—Spring 2005—2 [4] Programming Paradigms. Procedural Programming Object-Oriented Programming We program an abstraction of the Von Neumann Machine, Programming with objects that encapsulate data and consisting of a store (memory), a program (kept in the operations. store), A CPU and a program counter (PC): A variant of imperative programming first introduced Computing x:=x+1 1. Compute x’s ad- with the Norwegian language Simula in the mid 60s. CPU dress, send it to the Simula, Eiffel, Modula-3, C++. Addresses Data store, get x’s value back. Logic Programming X: 53 Jump 2. Add 1 to x’s value. Program: PC := PC + 8; PC Programming with relations. 3. Send x’s address Update Introduced in the early 70s. Based on predicate X := X + 1 store and new value to the calculus. Prolog is popular with Computational Store store for storage. Linguists. 4. Increment PC. Prolog, Parlog. 520—Spring 2005—2 [5] 520—Spring 2005—2 [6] Procedural Programming. Procedural Programming. The programmer... Procedural programming is difficult because: uses control structures (IF, WHILE, ...) to alter the 1. A procedural program can be in a large number of program counter (PC), states. (Any combination of variable values and PC uses assignment statements to alter the store. locations constitutes a possible state.) The programmer has to keep track of all of them. is in charge of memory management, i.e. declaring 2. Any global variable can be changed from any location in variables to hold values during the computation. the program. (This is particularly true of languages like § ¤ C & C++ [Why?]). function f a c t ( n : integer ) : integer ; 3. It is difficult to reason mathematically about a var s , i : integer : = 1 ; procedural program. begin while i <=n do s := s∗ i ; i := i + 1 ; end ; r e t u r n s ; end f a c t . ¦ ¥ 520—Spring 2005—2 [7] 520—Spring 2005—2 [8] Functional Programming Functional Languages In contrast to procedural languages, functional programs Common characteristics of functional programming don’t concern themselves with state and memory locations. languages: Instead, they work exclusively with values, and 1. Simple and concise syntax and semantics. expressions and functions which compute values. 2. Repetition is expressed as recursion rather than Functional programming is not tied to the von Neumann iteration. machine. 3. Functions are first class objects. I.e. functions can be It is not necessary to know anything about the manipulated just as easily as integers, floats, etc. in underlying hardware when writing a functional program, other languages. the way you do when writing an imperative program. 4. Data as functions. I.e. we can build a function on the Functional programs are more declarative than fly and then execute it. (Some languages). procedural ones; i.e. they describe what is to be computed rather than how it should be computed. 520—Spring 2005—2 [9] 520—Spring 2005—2 [10] Functional Languages. Functional Languages. 5. Higher-order functions. I.e. functions can take 8. Polymorphic types. Functions can work on data of functions as arguments and return functions as results. different types. (Some languages). 6. Lazy evaluation. Expressions are evaluated only when 9. Functional programs can be more easily needed. This allows us to build infinite data structures, manipulated mathematically than procedural programs. where only the parts we need are actually constructed. (Some languages). Pure vs. Impure FPL 7. Garbage Collection. Dynamic memory that is no longer Some functional languages are pure, i.e. they contain needed is automatically reclaimed by the system. GC is no imperative features at all. Examples: Haskell, also available in some imperative languages (Modula-3, Miranda, Gofer. Eiffel) but not in others (C, C++, Pascal). Impure languages may have assignment-statements, goto:s, while-loops, etc. Examples: LISP, ML, Scheme. 520—Spring 2005—2 [11] 520—Spring 2005—2 [12] What is a function? More on functions A function maps argument values (inputs) to result A function must not map an input value to values (outputs). more than one output value. Example: A function takes argument values from a source set (or True domain). A function produces result values that lie in a target set Jenny IsPretty (or range). False Source (Domain) Function Target (Range) not a Sweden Washington DC Capital function ! USA NZ Stockholm Wellington Copenhagen 520—Spring 2005—2 [13] 520—Spring 2005—2 [14] More on functions. More on functions. If a function F maps every element in the domain to A function that is undefined for some inputs, is called some element in the range, then F is total. I.e. a total partial. function is defined for all arguments. Int Int Divide Int Plus Int Int Int ? Divide is partial since 0 =? is undefined. 520—Spring 2005—2 [15] 520—Spring 2005—2 [16] Specifying functions Specifying functions. A function can be specified extensionally or intentionally. Intensionally: Extensionally: Give a rule (i.e. algorithm) that computes the result from the arguments. Enumerate the elements of the (often infinite) set of pairs “(argument, result)” or The intentional view emphasizes the process (or “Argument 7! Result.” algorithm) that is used to compute the result from the arguments. The extensional view emphasizes the external behavior (or specification), i.e. what the function does, rather double x = 2 * x than how it does it. even x = x mod 2 == 0 isHandsome x = if isBald x double = {· · ·, (1,2), (5,10), · · ·g then True even = {· · ·, (0,True), (1,False), · · ·g else False double = {· · ·, 17!2, 57!10, · · ·g isHandsome=fChris7!True,Hugh7!Falseg 520—Spring 2005—2 [17] 520—Spring 2005—2 [18] Specifying functions. Function Application Graphically: The most important operation in a functional program is The graphical view is a notational variant of the function application, i.e. applying an input argument to intentional view. the function, and retrieving the result: double x = 2 * x even even x = x mod 2 == 0 or even double 5 ) 10 even 6 ) True eithereven 520—Spring 2005—2 [19] 520—Spring 2005—2 [20] Function Composition Function Definition — Example Function composition makes the result of one function Example: How many numbers are there between m and n, application the input to another application: inclusive? Extensional Definition: double x = 2 * x sumbetween m n = {· · · (1; 1) 7! 1; (1; 2) 7! 2; · · · ; (2; 10) 7! 9g even x = x mod 2 == 0 Intentional Definition: sumbetween m n = ((m + n) * (abs (m-n) + 1)) div 2 even (double 5) ) even 10 ) True Graphical Definition: m + * n div − abs + 2 1 sumbetween 520—Spring 2005—2 [21] 520—Spring 2005—2 [22] Function Signatures Referential Transparency To define a function we must specify the types of the input The most important concept of functional programming and output sets (domain and range, i.e. the function’s is referential transparency. Consider the expression signature), and an algorithm that maps inputs to outputs. (2 ∗ 3) + 5 ∗ (2 ∗ 3) int The Signature! double int (2 ∗ 3) occurs twice in the expression, but it * has the same meaning (6) both times. 2 RT means that the value of a particular expression (or sub-expression) is always the same, regardless of where it occurs. int even Bool This concept occurs naturally in mathematics, but is broken by imperative programming languages. mod == RT makes functional programs easier to reason about 2 0 mathematically. 520—Spring 2005—2 [23] 520—Spring 2005—2 [24] Referential Transparency. Referential Transparency. RT is a fancy term for a very simple concept. Pure functional programming languages are What makes FP particularly simple, direct, and referentially transparent. expressive, is that there is only one mechanism This means that it is easy to find the meaning (value) of (function application) for communicating between an expression. subprograms. We can evaluate it by substitution. I.e. we can replace In imperative programs we can communicate either a function application by the function definition itself. through procedure arguments or updates of global variables. This is hard to reason about mathematically. A notation (programming language) where the value of an expression depends only on the values of the sub-expressions is called referentially transparent. 520—Spring 2005—2 [25] 520—Spring 2005—2 [26] Referential Transparency.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages9 Page
-
File Size-