* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Functional Programming
Curry–Howard correspondence wikipedia , lookup
C Sharp (programming language) wikipedia , lookup
Closure (computer programming) wikipedia , lookup
Anonymous function wikipedia , lookup
Falcon (programming language) wikipedia , lookup
Lambda calculus wikipedia , lookup
Standard ML wikipedia , lookup
Lambda calculus definition wikipedia , lookup
Functional Programming Yoonsik Cheon [email protected] CS 3360 Chapter 15 Spring 2011 Objectives Become familiar with the functional programming paradigm Know its foundation Understand how to write programs in Haskell 2 Outline Imperative vs. functional programming Functions and lambda calculus 3 Imperative Programming Relies on modifying on a state by using a sequence of commands The state is modified by assignment command. E.g., v = E or v := E. Commands can be: Sequenced, e.g., C1 ; C2 Conditionally executed, e.g., if Repeated, e.g., while Programs are a series of instructions on how to modify the state. Imperative (or procedural) languages, e.g., Fortran, Pascal, and C, support this style of programming. 4 An Abstract View A program execution can be modeled as a sequence of state changes starting from an initial state. s 0 s1 s 2 … sn 5 Functional Programming A functional program is simply an expression, and executing the program means evaluating the expression. On the positive side, one can have: No state, i.e., variables No assignment No sequencing and repetition Recursive functions Flexibility, e.g., higher-order functions Functional languages, e.g., Lisp, Scheme, SML, Haskell, support this style of programming. 6 Example --- Factorial In Mathematics, factorial function, f : N N, is defined as: f(x) = 1 x * f(x-1) if x = 0 otherwise In C, factorial function written imperatively as: int fact(int n) { int x = 1; while (n > 0) { x = x * n; n = n - 1; } return x; } 7 Example --- Factorial (Cont.) In Mathematics, factorial function, f : N N, is defined as: f(x) = 1 x * f(x-1) if x = 0 otherwise In Haskell fact 0 = 1 fact n = n * fact (n – 1) fact n | n == 0 = 1 | n > 0 = n * fact (n – 1) fact n | n == 0 = 1 | otherwise = n * fact (n – 1) 8 Merits of Functional Programming By avoiding variables and assignments: Clear semantics (tied to mathematical objects) More freedom in implementation, e.g., parallelization By the more flexible use of functions: Conciseness and elegance Better parameterization and modularity of programs Convenient ways of representing infinite data 9 Problems with Functional Programming Some things are harder to fit into a purely functional model, e.g., Input-output Interactive or continuously running programs (e.g., editors, process controllers) Functional languages also correspond less closely to current hardware, and thus they can be less efficient and it can be hard to reason about time and space usage. 10 In Sum, The design of the imperative languages is based directly on the von Neumann architecture Efficiency is the primary concern, rather than the suitability of the language for software development The design of the functional languages is based on mathematical functions A solid theoretical basis that is also closer to the user, but relatively unconcerned with the architecture of the machines on which programs will run 11 Outline Imperative vs. functional programming Functions and lambda calculus 12 Mathematical Functions A mathematical function is a mapping of members of one set, called the domain set, to another set, called the range set. 1 1 f: N+ N+ domain 100 10 Range 13 Higher-Order Functions A higher-order function, or functional form, is one that either takes functions as parameters or yields a function as its result, or both. Examples Function composition Construction Apply-to-all 14 Functional Composition A functional form that takes two functions as parameters and yields a function whose value is the first actual parameter function applied to the application of the second Form: h f ° g (or g ; f) which means h (x) f (g ( x)) For f (x) x * x * x and g (x) x + 3, h f ° g yields (x + 3)* (x + 3)* (x + 3) 15 Construction A functional form that takes a list of functions as parameters and yields a list of the results of applying each of its parameter functions to a given parameter Form: [|f, g|] For f (x) x * x * x and g (x) x + 3, [|f, g|] (4) yields [64, 7] 16 Apply-To-All A functional form that takes a single function as a parameter and yields a list of values obtained by applying the given function to each element of a list of parameters Form: For h (x) x * x * x (h, [3, 2, 4]) yields [27, 8, 64] 17 Lambda Calculus Lambda notation is a way of denoting functions Invented by Alonzo Church in the 1930s. x.E[x], ‘the function of x that yields E[x]’ Example x.x Squaring function? 18 Why Lambda? In informal mathematics, when talking about a function, one normally gives it a name, e.g., “define f(x) = E[x] … then … f …” Similarly, most programming languages will only let you define functions if you are prepared to give them a name. This approach is rather inconsistent, as we are not treating functions on a par with other mathematical objects. E.g., we don’t say “define x and y by x = 2 and y = 4 respectively. Then x*x = y.” Lambda notation helps to put functions on an equal footing. This is important in functional programming. 19 Example --- Currying Functions of more than one argument x.y.x + y Q: Signature? Q: (x.y.x + y) 1 2 Covention x y.E[x,y] means x.y.E[x,y] 20 Exercise (5 Minutes In Pairs) Evaluate the following expressions. (x.x) (x.x) (x.y.(y x)) 0 (x.x) (x.y.(x y)) ((x.x) (x.x)) (x.x) 21 Formal Lamda Calculus Syntax of lambda expressions <lambda-expr> -> <variable> | <constant> | <application> | <abstraction> <application> -> ( <lambda-expr> ) <lambda-expr> <abstraction> -> <variable> . <lambda-expr> Examples x, y, … 0, 1, false, true, … (x.x) 0, (x.x) false, … x.x, … 22 Exercise Extend the lambda expression syntax to include unary operators, binary operators, and if-then-else expression. <lambda-expr> -> … | <unary-opr> <lambda-expr> | <lambda-expr> <binary-opr> <lambda-expr> | if <lambda-expr> then <lambda-expr> else <lambda-expr> Examples -x, x + y, if x > 0 then 1 else -1, … 23 Exercise Define a lambda abstraction of the following function, f : R R, defined as f(x) = 0 x2sin(1/x2) if x = 0 if x != 0 24 Local Definitions <lambda-expr> -> … | let <name> = <lambda-expr> in <lambda-expr> Examples let x = 10 in x let incr = x.x+1 in incr 10 let f = x.if x == 0 then 1 else x * f (x – 1) in f 3 25 Group Exercise In lambda notation, define higher-order functions of functional composition, construction, and apply-to-all. For construction and apply-to-all, you may use Haskell lists operators and functions, such as: [] – empty list : – concatenation of element and list, e.g., 1 : [2] [1, 2] length – number of elements in a list, e.g., length [2, 3] 2 head – first element of a list, e.g., head [1, 2] 1 tail – list containing all elements except for the first, e.g., tail [1, 2, 3] [2, 3], tail [1] [] 26 In Sum, Fundamentals of FPL The objective of the design of a FPL is to mimic mathematical functions to the greatest extent possible. The basic process of computation is fundamentally different in a FPL than in an imperative language. In an imperative language, operations are done and the results are stored in variables for later use Management of variables is a constant concern and source of complexity for imperative programming. In an FPL, variables are not necessary, as is the case in mathematics. 27 Fundamentals of FPL (Cont.) In an FPL, the evaluation of a function always produces the same result given the same parameters. This is called referential transparency. 28