cs1022 computer programming & principles lecture 2 functions

24
CS1022 Computer Programming & Principles Lecture 2 Functions

Upload: gabriel-richard

Post on 23-Dec-2015

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS1022 Computer Programming & Principles Lecture 2 Functions

CS1022 Computer Programming &

Principles

Lecture 2Functions

Page 2: CS1022 Computer Programming & Principles Lecture 2 Functions

Plan of lecture• Inverse functions• Composition of functions• Pigeonhole principle• Fundamentals of functional programming

2CS1022

Page 3: CS1022 Computer Programming & Principles Lecture 2 Functions

• 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 Afor 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)

3CS1022

Page 4: CS1022 Computer Programming & Principles Lecture 2 Functions

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)

4CS1022

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!

Page 5: CS1022 Computer Programming & Principles Lecture 2 Functions

• Reversing arrows can be adapted for more complex scenarios (not graphic ones)

• Let’s 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)

5CS1022

Multiply by 4 Add 3 4x 34xx

Divide by 4

Subtract 3 xx 3¼ (x 3)

Page 6: CS1022 Computer Programming & Principles Lecture 2 Functions

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

6CS1022

Page 7: CS1022 Computer Programming & Principles Lecture 2 Functions

• 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 invertible2. Show that an invertible function must be bijective

Inverse functions (5)

7CS1022

Page 8: CS1022 Computer Programming & Principles Lecture 2 Functions

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

8CS1022

Page 9: CS1022 Computer Programming & Principles Lecture 2 Functions

• Let f : R R, f(x) x2 and g : R R, g(x) 4x 3Calculate g f

Calculate f g

Calculate f f

Calculate g g

Composition of functions (2)

9CS1022

g f(x) g f(x) g(f(x)) g f(x) g(f(x)) g(x2)g f(x) g(f(x)) g(x2) 4x2 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)2f g(x) f(g(x)) f(4x 3) (4x 3)2 16x2 24x 9

f f(y) f f(y) f(f(x))f f(y) f(f(x)) f(x2)f f(y) f(f(x)) f(x2) (x2)2f f(y) f(f(x)) f(x2) (x2)2 x4

g g(x) g(g(x)) g(4x 3) 4(4x 3) 3 16x 15

Page 10: CS1022 Computer Programming & Principles Lecture 2 Functions

• Let f : A B be a function over finite sets A and B• Suppose A a1, a2, , an (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(ai) f(aj), for some i and j, i j

The pigeonhole principle (1)

10CS1022

Page 11: CS1022 Computer Programming & Principles Lecture 2 Functions

• Reality check:– Suppose f(ai) f(aj), for all i and j, i j

– Then B contains n distinct elements f(a1), f(a2), , f(an)– Hence |B| n, which contradicts |A| |B|

Therefore there are at least two distinct elements ai, aj A with f(ai) f(aj)

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

11CS1022

something missing here?

Page 12: CS1022 Computer Programming & Principles Lecture 2 Functions

• Functions neatly capture input/output relation– For instance, f(x) x2 maps input x onto output x2

• Functional programming languages explore– Recursion to achieve repetition– Function composition for modularity

• We’ll look at these two features in the next slides

Functional programming (1)

12CS1022

Page 13: CS1022 Computer Programming & Principles Lecture 2 Functions

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

13CS1022

Page 14: CS1022 Computer Programming & Principles Lecture 2 Functions

• 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”: don’t worry about their implementation– They are available for ready use

Functional programming (3)

14CS1022

Page 15: CS1022 Computer Programming & Principles Lecture 2 Functions

• 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, outwardsf(g(h()))f(g(h()))

f(g(result))f(g(result))

f(result)result

Function composition (1)

15CS1022

Page 16: CS1022 Computer Programming & Principles Lecture 2 Functions

• Example: suppose s “bat”, compute result ofLEN(REST(s))

LEN(REST(“bat”))LEN(“at”)

2

Function composition (2)

16CS1022

Page 17: CS1022 Computer Programming & Principles Lecture 2 Functions

• Example: if s “bat”, compute result ofADDCHAR(CHAR(s), ADDCHAR(‘o’, REST(s)))ADDCHAR(CHAR(s), ADDCHAR(‘o’, REST(s)))

ADDCHAR(CHAR(s), ADDCHAR(‘o’, “at”))ADDCHAR(CHAR(s), ADDCHAR(‘o’, “at”))

ADDCHAR(CHAR(s), “oat”)ADDCHAR(CHAR(s), “oat”)

ADDCHAR(‘b’, “oat”)ADDCHAR(‘b’, “oat”)

“boat”

Function composition (3)

17CS1022

Page 18: CS1022 Computer Programming & Principles Lecture 2 Functions

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

18CS1022

Page 19: CS1022 Computer Programming & Principles Lecture 2 Functions

• (Most) functional programming languages do not have while-, repeat-until- or for-loops

Recursion (1)

19CS1022

• 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

Page 20: CS1022 Computer Programming & Principles Lecture 2 Functions

• Let us consider again the factorial of numbers:

• A functional solution in Python is

• How does it work?

Recursion (2)

20CS1022

def factorial(n): if n == 0: return 1 else: return factorial(n-1) * n

Page 21: CS1022 Computer Programming & Principles Lecture 2 Functions

Let’s “trace” how factorial(3) is computedRecursion (3)

21CS1022

n 3if n == 0: return 1else: return factorial(n 1) * n

n 2if n == 0: return 1else: return factorial(n 1) * n

n 1if n == 0: return 1else: return factorial(n 1) * n

n 0if n == 0: return 1else: return factorial(n 1) * n

n 1if n == 0: return 1else: return 1 * 1

n 2if n == 0: return 1else: return 1 * 2

n 3if n == 0: return 1else: return 2 * 3

6

Page 22: CS1022 Computer Programming & Principles Lecture 2 Functions

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

22CS1022

Page 23: CS1022 Computer Programming & Principles Lecture 2 Functions

You should now know:• Inverse functions• Pigeonhole principle• Principles of functional programming• Recursion!

Summary

23CS1022

Page 24: CS1022 Computer Programming & Principles Lecture 2 Functions

Further reading• R. Haggarty. “Discrete Mathematics for

Computing”. Pearson Education Ltd. 2002. (Chapter 5)

• Wikipedia’s entry• Wikibooks entry

24CS1022