Iteration and Invariants CoSc 450: Programming Paradigms 03 Tail - - PowerPoint PPT Presentation

iteration and invariants
SMART_READER_LITE
LIVE PREVIEW

Iteration and Invariants CoSc 450: Programming Paradigms 03 Tail - - PowerPoint PPT Presentation

CoSc 450: Programming Paradigms 03 Iteration and Invariants CoSc 450: Programming Paradigms 03 Tail recursion: What our author calls iteration is more commonly called tail recursion. A good optimizing compiler can convert a


slide-1
SLIDE 1

CoSc 450: Programming Paradigms

Iteration and Invariants

03

slide-2
SLIDE 2

CoSc 450: Programming Paradigms Tail recursion:

  • What our author calls “iteration” is more

commonly called “tail recursion”.

  • A good optimizing compiler can convert a tail

recursive program into an iterative one (as a loop). 03

slide-3
SLIDE 3

CoSc 450: Programming Paradigms Tail recursion:

  • What our author calls “iteration” is more

commonly called “tail recursion”.

  • A good optimizing compiler can convert a tail

recursive program into an iterative one (as a loop). 03

slide-4
SLIDE 4

CoSc 450: Programming Paradigms Tail recursion:

  • What our author calls “iteration” is more

commonly called “tail recursion”.

  • A good optimizing compiler can convert a tail

recursive program into an iterative one (as a loop). 03

slide-5
SLIDE 5

CoSc 450: Programming Paradigms Not tail recursion: 03

slide-6
SLIDE 6

CoSc 450: Programming Paradigms Not tail recursion: 03

n! = n ∗ (n − 1)!

slide-7
SLIDE 7

CoSc 450: Programming Paradigms Not tail recursion: 03

n! = n ∗ (n − 1)!

slide-8
SLIDE 8

CoSc 450: Programming Paradigms Not tail recursion: 03

n! = n ∗ (n − 1)!

Because after the recursive call additional processing must be done before the value is returned

slide-9
SLIDE 9

CoSc 450: Programming Paradigms General idea:

  • The function calls a helper function once.
  • The helper function is tail recursive, and calls

itself.

  • The helper function has an extra parameter.
  • The extra processing is done in the parameters
  • f the helper function.

03

slide-10
SLIDE 10

CoSc 450: Programming Paradigms General idea:

  • The function calls a helper function once.
  • The helper function is tail recursive, and calls

itself.

  • The helper function has an extra parameter.
  • The extra processing is done in the parameters
  • f the helper function.

03

slide-11
SLIDE 11

CoSc 450: Programming Paradigms General idea:

  • The function calls a helper function once.
  • The helper function is tail recursive, and calls

itself.

  • The helper function has an extra parameter.
  • The extra processing is done in the parameters
  • f the helper function.

03

slide-12
SLIDE 12

CoSc 450: Programming Paradigms General idea:

  • The function calls a helper function once.
  • The helper function is tail recursive, and calls

itself.

  • The helper function has an extra parameter.
  • The extra processing is done in the parameters
  • f the helper function.

03

slide-13
SLIDE 13

CoSc 450: Programming Paradigms General idea:

  • The function calls a helper function once.
  • The helper function is tail recursive, and calls

itself.

  • The helper function has an extra parameter.
  • The extra processing is done in the parameters
  • f the helper function.

03

slide-14
SLIDE 14

CoSc 450: Programming Paradigms Not tail recursion: 03

n! = n ∗ (n − 1)!

slide-15
SLIDE 15

CoSc 450: Programming Paradigms Not tail recursion: 03

n! = n ∗ (n − 1)!

Helper function, with two parameters a and b that computes a * b!

slide-16
SLIDE 16

CoSc 450: Programming Paradigms Not tail recursion: 03

n! = n ∗ (n − 1)!

Helper function, with two parameters a and b that computes a * b!

a · b! = (a · b) · (b − 1)!

slide-17
SLIDE 17

CoSc 450: Programming Paradigms Not tail recursion: 03

n! = n ∗ (n − 1)!

Helper function, with two parameters a and b that computes a * b!

a · b! = (a · b) · (b − 1)!

The main function calls the helper function with a value of one for a and n for b.

slide-18
SLIDE 18

CoSc 450: Programming Paradigms Write factorial and factorial-product. 03

slide-19
SLIDE 19

CoSc 450: Programming Paradigms Prove factorial-product is correct. 03

(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))

slide-20
SLIDE 20

CoSc 450: Programming Paradigms 03

(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))

Base case Code inspection: (factorial-product a 0) returns a. Math: Therefore, correct in base case.

a · 0! = a

slide-21
SLIDE 21

CoSc 450: Programming Paradigms 03

(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))

Base case Code inspection: (factorial-product a 0) returns a. Math: Therefore, correct in base case.

a · 0! = a

slide-22
SLIDE 22

CoSc 450: Programming Paradigms 03

(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))

Base case Code inspection: (factorial-product a 0) returns a. Math: Therefore, correct in base case.

a · 0! = a

slide-23
SLIDE 23

CoSc 450: Programming Paradigms 03

(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))

Base case Code inspection: (factorial-product a 0) returns a. Math: Therefore, correct in base case.

a · 0! = a

slide-24
SLIDE 24

CoSc 450: Programming Paradigms 03

(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))

Inductive case Prove that (factorial-product x b) terminates with value assuming that (factorial-product y (- b 1)) terminates with value as the inductive hypothesis.

x · b! y · (b − 1)!

slide-25
SLIDE 25

CoSc 450: Programming Paradigms 03

(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))

Inductive case Prove that (factorial-product x b) terminates with value assuming that (factorial-product y (- b 1)) terminates with value as the inductive hypothesis.

x · b! y · (b − 1)!

slide-26
SLIDE 26

CoSc 450: Programming Paradigms 03

(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))

Inductive case Prove that (factorial-product x b) terminates with value assuming that (factorial-product y (- b 1)) terminates with value as the inductive hypothesis.

x · b! y · (b − 1)!

slide-27
SLIDE 27

CoSc 450: Programming Paradigms 03

(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))

Inductive case

  • =
  • =
  • (a·b)·(b−1)!

=

  • a·b·(b−1)!

=

  • a·b!
slide-28
SLIDE 28

CoSc 450: Programming Paradigms 03

(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))

Inductive case

  • =
  • =
  • (a·b)·(b−1)!

=

  • a·b·(b−1)!

=

  • a·b!
slide-29
SLIDE 29

CoSc 450: Programming Paradigms 03

(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))

Inductive case

  • =
  • =
  • (a·b)·(b−1)!

=

  • a·b·(b−1)!

=

  • a·b!
slide-30
SLIDE 30

CoSc 450: Programming Paradigms 03

(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))

Inductive case

  • =
  • =
  • (a·b)·(b−1)!

=

  • a·b·(b−1)!

=

  • a·b!
slide-31
SLIDE 31

CoSc 450: Programming Paradigms 03

(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))

Inductive case

  • =
  • =
  • (a·b)·(b−1)!

=

  • a·b·(b−1)!

=

  • a·b!
slide-32
SLIDE 32

CoSc 450: Programming Paradigms 03

(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))

Inductive case

  • =
  • =
  • (a·b)·(b−1)!

=

  • a·b·(b−1)!

=

  • a·b!
slide-33
SLIDE 33

CoSc 450: Programming Paradigms 03

(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))

Inductive case

  • =
  • =
  • (a·b)·(b−1)!

=

  • a·b·(b−1)!

=

  • a·b!
slide-34
SLIDE 34

CoSc 450: Programming Paradigms 03

(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))

Inductive case

  • =
  • =
  • (a·b)·(b−1)!

=

  • a·b·(b−1)!

=

  • a·b!
slide-35
SLIDE 35

CoSc 450: Programming Paradigms 03

(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))

Inductive case

  • =
  • =
  • (a·b)·(b−1)!

=

  • a·b·(b−1)!

=

  • a·b!
slide-36
SLIDE 36

CoSc 450: Programming Paradigms 03

(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))

Inductive case

  • =
  • =
  • (a·b)·(b−1)!

=

  • a·b·(b−1)!

=

  • a·b!
slide-37
SLIDE 37

CoSc 450: Programming Paradigms 03 The power function

> (power 4 5) 1024 (power 4 5) returns 4 to the power 5.

slide-38
SLIDE 38

CoSc 450: Programming Paradigms 03 The power function

> (power 4 5) 1024 (power 4 5) returns 4 to the power 5.

slide-39
SLIDE 39

CoSc 450: Programming Paradigms 03 The power function

> (power 4 5) 1024 (power 4 5) returns 4 to the power 5.

slide-40
SLIDE 40

CoSc 450: Programming Paradigms Not tail recursion: 03

be = b · be−1

slide-41
SLIDE 41

CoSc 450: Programming Paradigms Not tail recursion: 03

be = b · be−1

Tail recursion:

(a) · be = (ab) · be−1

slide-42
SLIDE 42

CoSc 450: Programming Paradigms Write power and power-product. 03

slide-43
SLIDE 43

CoSc 450: Programming Paradigms 03 Exponentiation is not associative

(34)5 6= 3(45) 320 6= 31024

slide-44
SLIDE 44

CoSc 450: Programming Paradigms 03 Exponentiation is not associative

(34)5 6= 3(45) 320 6= 31024

slide-45
SLIDE 45

CoSc 450: Programming Paradigms 03 Fermat numbers

Fn = 2(2n) + 1

The first few Fermat numbers

n = 0 ⇒ 2 + 1 = 3 n = 1 ⇒ 22 + 1 = 5 n = 2 ⇒ (22)2 + 1 = 17 n = 3 ⇒ ((22)2)2 + 1 = 257

slide-46
SLIDE 46

CoSc 450: Programming Paradigms 03 Fermat numbers

Fn = 2(2n) + 1

The first few Fermat numbers

n = 0 ⇒ 2 + 1 = 3 n = 1 ⇒ 22 + 1 = 5 n = 2 ⇒ (22)2 + 1 = 17 n = 3 ⇒ ((22)2)2 + 1 = 257

slide-47
SLIDE 47

CoSc 450: Programming Paradigms 03 Fermat numbers

Fn = 2(2n) + 1

The first few Fermat numbers

n = 0 ⇒ 2 + 1 = 3 n = 1 ⇒ 22 + 1 = 5 n = 2 ⇒ (22)2 + 1 = 17 n = 3 ⇒ ((22)2)2 + 1 = 257

slide-48
SLIDE 48

CoSc 450: Programming Paradigms 03 Fermat numbers

Fn = 2(2n) + 1

The first few Fermat numbers

n = 0 ⇒ 2 + 1 = 3 n = 1 ⇒ 22 + 1 = 5 n = 2 ⇒ (22)2 + 1 = 17 n = 3 ⇒ ((22)2)2 + 1 = 257

slide-49
SLIDE 49

CoSc 450: Programming Paradigms 03 Fermat numbers

Fn = 2(2n) + 1

The first few Fermat numbers

n = 0 ⇒ 2 + 1 = 3 n = 1 ⇒ 22 + 1 = 5 n = 2 ⇒ (22)2 + 1 = 17 n = 3 ⇒ ((22)2)2 + 1 = 257

slide-50
SLIDE 50

CoSc 450: Programming Paradigms 03 Fermat numbers

Fn = 2(2n) + 1

The first few Fermat numbers

n = 0 ⇒ 2 + 1 = 3 n = 1 ⇒ 22 + 1 = 5 n = 2 ⇒ (22)2 + 1 = 17 n = 3 ⇒ ((22)2)2 + 1 = 257

slide-51
SLIDE 51

CoSc 450: Programming Paradigms 03 Fermat numbers

n = 3 ⇒ ((22)2)2 | {z } +1 = 257

2 repeatedly squared 3 times

(repeatedly-square 2 0) should return (repeatedly-square 2 1) should return (repeatedly-square 2 2) should return

2 22 = 4 (22)2 = 16

slide-52
SLIDE 52

CoSc 450: Programming Paradigms 03 Fermat numbers

n = 3 ⇒ ((22)2)2 | {z } +1 = 257

2 repeatedly squared 3 times

(repeatedly-square 2 0) should return (repeatedly-square 2 1) should return (repeatedly-square 2 2) should return

2 22 = 4 (22)2 = 16

slide-53
SLIDE 53

CoSc 450: Programming Paradigms 03 Fermat numbers

n = 3 ⇒ ((22)2)2 | {z } +1 = 257

2 repeatedly squared 3 times

(repeatedly-square 2 0) should return (repeatedly-square 2 1) should return (repeatedly-square 2 2) should return

2 22 = 4 (22)2 = 16

slide-54
SLIDE 54

CoSc 450: Programming Paradigms 03 Fermat numbers

n = 3 ⇒ ((22)2)2 | {z } +1 = 257

2 repeatedly squared 3 times

(repeatedly-square 2 0) should return (repeatedly-square 2 1) should return (repeatedly-square 2 2) should return

2 22 = 4 (22)2 = 16

slide-55
SLIDE 55

CoSc 450: Programming Paradigms 03 Fermat numbers

n = 3 ⇒ ((22)2)2 | {z } +1 = 257

2 repeatedly squared 3 times

(repeatedly-square 2 0) should return (repeatedly-square 2 1) should return (repeatedly-square 2 2) should return

2 22 = 4 (22)2 = 16

slide-56
SLIDE 56

CoSc 450: Programming Paradigms 03 Fermat numbers

  • b(2n)

= 2n = 2·2n−1 b2·(2n−1) = xy·z = (xy)z (b2)2n−1 b n b2 n−1

slide-57
SLIDE 57

CoSc 450: Programming Paradigms 03 Fermat numbers

  • b(2n)

= 2n = 2·2n−1 b2·(2n−1) = xy·z = (xy)z (b2)2n−1 b n b2 n−1

slide-58
SLIDE 58

CoSc 450: Programming Paradigms 03 Fermat numbers

  • b(2n)

= 2n = 2·2n−1 b2·(2n−1) = xy·z = (xy)z (b2)2n−1 b n b2 n−1

slide-59
SLIDE 59

CoSc 450: Programming Paradigms 03 Fermat numbers

  • b(2n)

= 2n = 2·2n−1 b2·(2n−1) = xy·z = (xy)z (b2)2n−1 b n b2 n−1

slide-60
SLIDE 60

CoSc 450: Programming Paradigms 03 Fermat numbers

  • b(2n)

= 2n = 2·2n−1 b2·(2n−1) = xy·z = (xy)z (b2)2n−1 b n b2 n−1

slide-61
SLIDE 61

CoSc 450: Programming Paradigms 03 Fermat numbers

  • b(2n)

= 2n = 2·2n−1 b2·(2n−1) = xy·z = (xy)z (b2)2n−1 b n b2 n−1

slide-62
SLIDE 62

CoSc 450: Programming Paradigms Write fermat-number and repeatedly-square. 03

slide-63
SLIDE 63

CoSc 450: Programming Paradigms 03 Perfect number

  • The sum of its divisors is twice the number, or
  • The number is equal to the sum of its divisors
  • ther than itself.
slide-64
SLIDE 64

CoSc 450: Programming Paradigms 03 Perfect number

  • The sum of its divisors is twice the number, or
  • The number is equal to the sum of its divisors
  • ther than itself.

2·6 = 1+2+3+6 6 = 1+2+3

slide-65
SLIDE 65

CoSc 450: Programming Paradigms 03 Perfect number

  • The sum of its divisors is twice the number, or
  • The number is equal to the sum of its divisors
  • ther than itself.

2·6 = 1+2+3+6 6 = 1+2+3

There are no known odd perfect numbers.

slide-66
SLIDE 66

CoSc 450: Programming Paradigms 03 Suppose you have a function sum-of-divisors such that (sum-of-divisors 4) returns 1+2+4, (sum-of-divisors 5) returns 1+5, (sum-of-divisors 6) returns 1+2+3+6, etc.

slide-67
SLIDE 67

CoSc 450: Programming Paradigms 03 Suppose you have a function sum-of-divisors such that (sum-of-divisors 4) returns 1+2+4, (sum-of-divisors 5) returns 1+5, (sum-of-divisors 6) returns 1+2+3+6, etc. Write perfect? such that (perfect? 4) returns #f, (perfect? 5) returns #f, (perfect? 6) returns #t, etc.

slide-68
SLIDE 68

CoSc 450: Programming Paradigms 03 The shape of sum-of-divisors

(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0)))

slide-69
SLIDE 69

CoSc 450: Programming Paradigms 03 The shape of sum-of-divisors

(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0)))

sum-from-plus is defined

inside sum-of-divisors. So, it has access to n.

slide-70
SLIDE 70

CoSc 450: Programming Paradigms 03 The shape of sum-of-divisors

(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0)))

sum-from-plus is defined

inside sum-of-divisors. So, it has access to n.

low addend

slide-71
SLIDE 71

CoSc 450: Programming Paradigms 03 The shape of sum-of-divisors

(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0)))

sum-from-plus returns the sum of all the divisors

  • f n that are greater than or equal to low

plus the addend.

low addend

slide-72
SLIDE 72

CoSc 450: Programming Paradigms 03

sum-from-plus returns the sum of all the divisors

  • f n that are greater than or equal to low

plus the addend.

slide-73
SLIDE 73

CoSc 450: Programming Paradigms 03

sum-from-plus returns the sum of all the divisors

  • f n that are greater than or equal to low

plus the addend. Is 12 perfect?

slide-74
SLIDE 74

CoSc 450: Programming Paradigms 03

sum-from-plus returns the sum of all the divisors

  • f n that are greater than or equal to low

plus the addend. Is 12 perfect? 1 2 3 4 5 6 7 8 9 10 11 12

slide-75
SLIDE 75

CoSc 450: Programming Paradigms 03

sum-from-plus returns the sum of all the divisors

  • f n that are greater than or equal to low

plus the addend. Is 12 perfect? 1 2 3 4 5 6 7 8 9 10 11 12

n = 12

slide-76
SLIDE 76

CoSc 450: Programming Paradigms 03

sum-from-plus returns the sum of all the divisors

  • f n that are greater than or equal to low

plus the addend. Is 12 perfect? 1 2 3 4 5 6 7 8 9 10 11 12

low = 6 n = 12

slide-77
SLIDE 77

CoSc 450: Programming Paradigms 03

sum-from-plus returns the sum of all the divisors

  • f n that are greater than or equal to low

plus the addend. Is 12 perfect? 1 2 3 4 5 6 7 8 9 10 11 12

addend = 1+2+3+4 low = 6 n = 12

slide-78
SLIDE 78

CoSc 450: Programming Paradigms 03

sum-from-plus returns the sum of all the divisors

  • f n that are greater than or equal to low

plus the addend. Is 12 perfect? 1 2 3 4 5 6 7 8 9 10 11 12

addend = 1+2+3+4 low = 6 n = 12 (sum-from-plus 6 10)

slide-79
SLIDE 79

CoSc 450: Programming Paradigms 03

sum-of-divisors

(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend

slide-80
SLIDE 80

CoSc 450: Programming Paradigms 03

(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend

sum-of-divisors

slide-81
SLIDE 81

CoSc 450: Programming Paradigms 03

(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend

sum-of-divisors

slide-82
SLIDE 82

CoSc 450: Programming Paradigms 03

(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend

sum-of-divisors

slide-83
SLIDE 83

CoSc 450: Programming Paradigms 03

(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend

sum-of-divisors

slide-84
SLIDE 84

CoSc 450: Programming Paradigms 03

(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend

sum-of-divisors

slide-85
SLIDE 85

CoSc 450: Programming Paradigms 03

(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend

sum-of-divisors

slide-86
SLIDE 86

CoSc 450: Programming Paradigms 03

(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend

sum-of-divisors

slide-87
SLIDE 87

CoSc 450: Programming Paradigms 03 Hallmarks of pure functional programming

  • A function returns a value.
  • There is no call by reference.
  • All parameters are called by value.
  • There are no loops.
  • Repetition is achieved by recursion.
  • There is no assignment statement.
slide-88
SLIDE 88

CoSc 450: Programming Paradigms 03 The Golden Ratio

slide-89
SLIDE 89

CoSc 450: Programming Paradigms 03 The Golden Ratio

slide-90
SLIDE 90

CoSc 450: Programming Paradigms 03 The Golden Ratio A A

slide-91
SLIDE 91

CoSc 450: Programming Paradigms 03 The Golden Ratio A B A

slide-92
SLIDE 92

CoSc 450: Programming Paradigms 03 The Golden Ratio A B A A B = A + B A = 1 + B A = 1 + 1 A/B φ = A/B φ = 1 + 1 φ

slide-93
SLIDE 93

CoSc 450: Programming Paradigms 03 The Golden Ratio A B A A B = A + B A = 1 + B A = 1 + 1 A/B φ = A/B φ = 1 + 1 φ

slide-94
SLIDE 94

CoSc 450: Programming Paradigms 03 The Golden Ratio A B A A B = A + B A = 1 + B A = 1 + 1 A/B φ = A/B φ = 1 + 1 φ

slide-95
SLIDE 95

CoSc 450: Programming Paradigms 03 The Golden Ratio A B A A B = A + B A = 1 + B A = 1 + 1 A/B φ = A/B φ = 1 + 1 φ

slide-96
SLIDE 96

CoSc 450: Programming Paradigms 03 The Golden Ratio A B A A B = A + B A = 1 + B A = 1 + 1 A/B φ = A/B φ = 1 + 1 φ

slide-97
SLIDE 97

CoSc 450: Programming Paradigms 03 Successive approximations of the Golden Ratio

slide-98
SLIDE 98

CoSc 450: Programming Paradigms 03 Successive approximations of the Golden Ratio

φ0 = 1 φ1 = 1 + 1 φ0 = 2 φ2 = 1 + 1 φ1 = 3 2 φ3 = 1 + 1 φ2 = 5 3 φ4 = 1 + 1 φ3 = 8 5

slide-99
SLIDE 99

CoSc 450: Programming Paradigms 03 Successive approximations of the Golden Ratio

φ0 = 1 φ1 = 1 + 1 φ0 = 2 φ2 = 1 + 1 φ1 = 3 2 φ3 = 1 + 1 φ2 = 5 3 φ4 = 1 + 1 φ3 = 8 5

slide-100
SLIDE 100

CoSc 450: Programming Paradigms 03 Successive approximations of the Golden Ratio

φ0 = 1 φ1 = 1 + 1 φ0 = 2 φ2 = 1 + 1 φ1 = 3 2 φ3 = 1 + 1 φ2 = 5 3 φ4 = 1 + 1 φ3 = 8 5

slide-101
SLIDE 101

CoSc 450: Programming Paradigms 03 Successive approximations of the Golden Ratio

φ0 = 1 φ1 = 1 + 1 φ0 = 2 φ2 = 1 + 1 φ1 = 3 2 φ3 = 1 + 1 φ2 = 5 3 φ4 = 1 + 1 φ3 = 8 5

slide-102
SLIDE 102

CoSc 450: Programming Paradigms 03 Successive approximations of the Golden Ratio

φ0 = 1 φ1 = 1 + 1 φ0 = 2 φ2 = 1 + 1 φ1 = 3 2 φ3 = 1 + 1 φ2 = 5 3 φ4 = 1 + 1 φ3 = 8 5

slide-103
SLIDE 103

CoSc 450: Programming Paradigms 03 Successive approximations of the Golden Ratio (phi 0) returns 1, (phi 1) returns 2, (phi 2) returns 3/2, (phi 3) returns 5/3, (phi 4) returns 8/5, etc. Write phi

slide-104
SLIDE 104

CoSc 450: Programming Paradigms 03 The Josephus Problem Rule: Everybody stands in a circle. Starting with the first, kill every third person. Which two people remain alive?

slide-105
SLIDE 105

CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6

slide-106
SLIDE 106

CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6

slide-107
SLIDE 107

CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6

slide-108
SLIDE 108

CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6

slide-109
SLIDE 109

CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6

slide-110
SLIDE 110

CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6

slide-111
SLIDE 111

CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6

slide-112
SLIDE 112

CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6

(survives? 4 8) returns #t (survives? 7 8) returns #t (survives? k 8) returns #f

for all other values of k

slide-113
SLIDE 113

CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6

(survives? 4 8) returns #t (survives? 7 8) returns #t (survives? k 8) returns #f

for all other values of k

slide-114
SLIDE 114

CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6

(survives? 4 8) returns #t (survives? 7 8) returns #t (survives? k 8) returns #f

for all other values of k

slide-115
SLIDE 115

CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6

(survives? 4 8) returns #t (survives? 7 8) returns #t (survives? k 8) returns #f

for all other values of k

Exercise for the student: Write survives?

slide-116
SLIDE 116

CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6

Hint: Parentheses are the new numbers after the first person is killed.

(survives? 6 8)

is recursively the same as

(survives? 3 7)

(1) (2) (3) (4) (5) (6) (7)

slide-117
SLIDE 117

CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6

Hint: Parentheses are the new numbers after the first person is killed.

(survives? 6 8)

is recursively the same as

(survives? 3 7)

(1) (2) (3) (4) (5) (6) (7)