Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functional Programming

Similar presentations


Presentation on theme: "Functional Programming"— Presentation transcript:

1 Functional Programming
Yoonsik Cheon CS Chapter Spring 2011

2 Objectives Become familiar with the functional programming paradigm
Know its foundation Understand how to write programs in Haskell

3 Outline Imperative vs. functional programming
Functions and lambda calculus

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

5 An Abstract View A program execution can be modeled as a sequence of state changes starting from an initial state. s0  s1  s2  …  sn

6 Functional Programming
A functional program is simply an expression, and executing the program means evaluating the expression. No state, i.e., variables No assignment No sequencing and repetition On the positive side, one can have: Recursive functions Flexibility, e.g., higher-order functions Functional languages, e.g., Lisp, Scheme, SML, Haskell, support this style of programming.

7 Example --- Factorial In Mathematics, factorial function, f : N  N, is defined as: if x = 0 x * f(x-1) otherwise f(x) = 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;

8 Example --- Factorial (Cont.)
In Mathematics, factorial function, f : N  N, is defined as: if x = 0 x * f(x-1) otherwise f(x) = 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)

9 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

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

11 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

12 Outline Imperative vs. functional programming
Functions and lambda calculus

13 Mathematical Functions
A mathematical function is a mapping of members of one set, called the domain set, to another set, called the range set. f: N+  N+ 1 1 10 100 domain Range

14 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

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

16 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]

17 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]

18 Lambda Calculus Lambda notation is a way of denoting functions Example
Invented by Alonzo Church in the 1930s. x.E[x], ‘the function of x that yields E[x]’ Example x.x Squaring function?

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

20 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]

21 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

22 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, …

23 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, …

24 Exercise Define a lambda abstraction of the following function, f : R  R, defined as f(x) = if x = 0 x2sin(1/x2) if x != 0 \x.if x == 0 then 0 else x * x * sin (1 / (x*x))

25 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

26 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]  [] \f.\g.\x.f (g x) let c = \l.\x.if (length l) == 0 then [] else (head l x) : c (tail l) x in c let a = \f.\l.if (length l) == 0 then [] else (f (head l)) : a f (tail l) in a

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

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


Download ppt "Functional Programming"

Similar presentations


Ads by Google