# cs1022 computer programming & principles lecture 2 functions

Post on 23-Dec-2015

218 views

Category:

## Documents

Embed Size (px)

TRANSCRIPT

• Slide 1
• CS1022 Computer Programming & Principles Lecture 2 Functions
• Slide 2
• Plan of lecture Inverse functions Composition of functions Pigeonhole principle Fundamentals of functional programming 2 CS1022
• Slide 3
• Any function f : A B is also a relation, so we can form the inverse relation f 1 If f 1 is a function, then f is an invertible function We write f 1 : B A for the inverse function Function f consists of pairs (a, b), f(a) b If f is invertible f 1 consists of pairs (b, a), f 1 (b) a That is, the inverse function reverses the effect of the original function, f(a) b, f 1 (b) a Inverse functions (1) 3 CS1022
• Slide 4
• a b 1 3 2 c a b 1 3 2 c a b 1 3 2 c Which of the functions are invertible? Inverse functions (2) 4 CS1022 a b 1 3 2 c a b 1 3 2 c a b 1 3 2 c Not a function! A function! Not a function!
• Slide 5
• Reversing arrows can be adapted for more complex scenarios (not graphic ones) Lets consider k : R R, k(x) = 4x 3 Its effect can be described by the following diagram Elementary operations multiply by 4 and add 3 reversed as divide by 4 and subtract 3, respectively Hence, k 1 : R R, k 1 (x) (x 3) Inverse functions (3) 5 CS1022 Multiply by 4 Add 3 4x 3 4x4xx Divide by 4 Subtract 3 x x 3 (x 3)
• Slide 6
• Solution of previous slide can also be obtained algebraically: Let y k(x), so x k 1 (y) Since y 4x 3, we can operate on it (y) 3 (4x 3) 3 (subtract 3 from both sides) (y 3) (4x) (divide both sides by 4) (y 3) x Therefore k 1 (y) (y 3) Since we usually use x as input parameter (a convention, but not essential), we have k 1 (x) (x 3) Inverse functions (4) 6 CS1022
• Slide 7
• Only bijective functions are invertible Elements of co-domain must have a unique element in domain (injective) Each element of co-domain is a value of the function (surjective) Theorem: f is invertible if, and only if, it is bijective Proof: in two parts 1.Show that a bijective function is invertible 2.Show that an invertible function must be bijective Inverse functions (5) 7 CS1022
• Slide 8
• Easier (more intuitive) than composition of relations Let f : A B and g : B C be functions Composite function g f : A C consists of (a, c) where for some b B, (a, b) f, and (b, c) g Notice: b f(a) uniquely determined by a since f is a function c g(b) uniquely determined by b since g is a function Hence, c g(f(a)) is uniquely determined by a So, the composition g f is also a function Therefore, g f : A C is function (g f )(x) g(f(a)) Composition of functions (1) 8 CS1022
• Slide 9
• Let f : R R, f(x) x 2 and g : R R, g(x) 4x 3 Calculate g f Calculate f g Calculate f f Calculate g g Composition of functions (2) 9 CS1022 g f(x) g f(x) g(f(x))g f(x) g(f(x)) g(x 2 )g f(x) g(f(x)) g(x 2 ) 4x 2 3 f g(x) f g(x) f(g(x))f g(x) f(g(x)) f(4x 3)f g(x) f(g(x)) f(4x 3) (4x 3) 2 f g(x) f(g(x)) f(4x 3) (4x 3) 2 16x 2 24x 9 f f(y) f f(y) f(f(x))f f(y) f(f(x)) f(x 2 )f f(y) f(f(x)) f(x 2 ) (x 2 ) 2 f f(y) f(f(x)) f(x 2 ) (x 2 ) 2 x 4 g g(x) g(g(x)) g(4x 3) 4(4x 3) 3 16x 15
• Slide 10
• Let f : A B be a function over finite sets A and B Suppose A a 1, a 2, , a n (with n elements) The pigeonhole principle states that if |A| |B| then at least one value of f occurs more than once That is, f(a i ) f(a j ), for some i and j, i j The pigeonhole principle (1) 10 CS1022
• Slide 11
• Reality check: Suppose f(a i ) f(a j ), for all i and j, i j Then B contains n distinct elements f(a 1 ), f(a 2 ), , f(a n ) Hence |B| n, which contradicts |A| |B| Therefore there are at least two distinct elements a i, a j A with f(a i ) f(a j ) Example: Any group with 13 or more people, will have two (or more) people with a birthday in the same month The pigeonhole principle (2) 11 CS1022 something missing here?
• Slide 12
• Functions neatly capture input/output relation For instance, f(x) x 2 maps input x onto output x 2 Functional programming languages explore Recursion to achieve repetition Function composition for modularity Well look at these two features in the next slides Functional programming (1) 12 CS1022
• Slide 13
• Example: simple text-processing scenario We use the following sets to define (co-)domains The natural numbers N = 0, 1, 2, The set C = {a, b, , z} of lower-case characters in English (no special characters such as , , etc.) The set S of strings of characters from C bat C C (empty string) Functional programming (2) 13 CS1022
• Slide 14
• Suppose the following primitive (built-in) functions CHAR : S C, where CHAR(s) is first character of s (non- empty) REST : S S, where REST(s) is the string obtained by removing the first character of s (non-empty) ADDCHAR : C S S, where ADDCHAR(c, s) is the string obtained by adding character c to the front of s LEN : S N, where LEN(s) is the number of characters in the string s Built-ins: dont worry about their implementation They are available for ready use Functional programming (3) 14 CS1022
• Slide 15
• We can compose basic built-in functions to define more sophisticated computations Composition by nesting functions f(g(h( ))) Important: expected type of parameters Evaluation from innermost function, outwards f(g(h( ))) f(g(result)) f(result) result Function composition (1) 15 CS1022
• Slide 16
• Example: suppose s bat, compute result of LEN(REST(s)) LEN(REST(bat)) LEN(at) 2 Function composition (2) 16 CS1022
• Slide 17
• Slide 18
• Notice: the value of s does not change Parameters are input, processed (without changes) and results output There is no assignment of values to variables At least not in the standard way... No destructive assignment as in x := x + 1 Function composition (4) 18 CS1022
• Slide 19
• (Most) functional programming languages do not have while-, repeat-until- or for-loops Recursion (1) 19 CS1022 Repetition achieved with recursion A function is defined in terms of itself A function calls itself Some non-functional programming languages also allow recursion Recursion can be most natural way to define some computations
• Slide 20
• Let us consider again the factorial of numbers: A functional solution in Python is How does it work? Recursion (2) 20 CS1022 def factorial(n): if n == 0: return 1 else: return factorial(n-1) * n
• Slide 21
• Lets trace how factorial(3) is computed Recursion (3) 21 CS1022 n 3 if n == 0: return 1 else: return factorial(n 1) * n n 2 if n == 0: return 1 else: return factorial(n 1) * n n 1 if n == 0: return 1 else: return factorial(n 1) * n n 0 if n == 0: return 1 else: return factorial(n 1) * n n 1 if n == 0: return 1 else: return 1 * 1 n 2 if n == 0: return 1 else: return 1 * 2 n 3 if n == 0: return 1 else: return 2 * 3 6
• Slide 22
• Notice: it requires a stack of function calls If too many repetitions, then we run out of memory In some programming languages (Haskell) when the recursive call is the last command (tail-recursive), then (in many cases) no stack is needed Performance is comparable to conventional loops Recursion can be indirect Function f defined in terms of function g, Function g defined in terms of function f Termination, just like in other loops, is an issue What would happen to factorial( 1)? Recursion (4) 22 CS1022
• Slide 23
• You should now know: Inverse functions Pigeonhole principle Principles of functional programming Recursion! Summary 23 CS1022
• Slide 24
• Further reading R. Haggarty. Discrete Mathematics for Computing. Pearson Education Ltd. 2002. (Chapter 5) Wikipedias entry Wikibooks entry 24 CS1022