Higher-Order Procedures CoSc 450: Programming Paradigms 05 In the - - PowerPoint PPT Presentation

higher order procedures cosc 450 programming paradigms 05
SMART_READER_LITE
LIVE PREVIEW

Higher-Order Procedures CoSc 450: Programming Paradigms 05 In the - - PowerPoint PPT Presentation

CoSc 450: Programming Paradigms 05 Higher-Order Procedures CoSc 450: Programming Paradigms 05 In the functional paradigm, functions themselves can be processed as data. CoSc 450: Programming Paradigms 05 In the functional paradigm,


slide-1
SLIDE 1

CoSc 450: Programming Paradigms

Higher-Order Procedures

05

slide-2
SLIDE 2

CoSc 450: Programming Paradigms In the functional paradigm, functions themselves can be processed as data. 05

slide-3
SLIDE 3

CoSc 450: Programming Paradigms In the functional paradigm, functions themselves can be processed as data. 05 In the same way you can pass data values as parameters in a function, you can pass function as a parameter in another function.

slide-4
SLIDE 4

CoSc 450: Programming Paradigms

(define power (lambda (b e) (if (= e 1) b (* (power b (- e 1)) b)))) (define stack-copies-of (lambda (quantity image) (if (= quantity 1) image (stack (stack-copies-of (- quantity 1) image) image))))

05

slide-5
SLIDE 5

CoSc 450: Programming Paradigms

(define power (lambda (b e) (if (= e 1) b (* (power b (- e 1)) b)))) (define stack-copies-of (lambda (quantity image) (if (= quantity 1) image (stack (stack-copies-of (- quantity 1) image) image))))

05 How are the functions similar?

slide-6
SLIDE 6

CoSc 450: Programming Paradigms

(define power (lambda (b e) (if (= e 1) b (* (power b (- e 1)) b)))) (define stack-copies-of (lambda (quantity image) (if (= quantity 1) image (stack (stack-copies-of (- quantity 1) image) image))))

05 How are the functions similar?

slide-7
SLIDE 7

CoSc 450: Programming Paradigms

(define power (lambda (b e) (if (= e 1) b (* (power b (- e 1)) b)))) (define stack-copies-of (lambda (quantity image) (if (= quantity 1) image (stack (stack-copies-of (- quantity 1) image) image))))

05 How are the functions similar?

slide-8
SLIDE 8

CoSc 450: Programming Paradigms

(define power (lambda (b e) (if (= e 1) b (* (power b (- e 1)) b)))) (define stack-copies-of (lambda (quantity image) (if (= quantity 1) image (stack (stack-copies-of (- quantity 1) image) image))))

05 How are the functions similar?

slide-9
SLIDE 9

CoSc 450: Programming Paradigms

(define power (lambda (b e) (if (= e 1) b (* (power b (- e 1)) b)))) (define stack-copies-of (lambda (quantity image) (if (= quantity 1) image (stack (stack-copies-of (- quantity 1) image) image))))

05 How are the functions similar?

slide-10
SLIDE 10

CoSc 450: Programming Paradigms

(define power (lambda (b e) (if (= e 1) b (* (power b (- e 1)) b)))) (define stack-copies-of (lambda (quantity image) (if (= quantity 1) image (stack (stack-copies-of (- quantity 1) image) image))))

05 How are the functions similar?

slide-11
SLIDE 11

CoSc 450: Programming Paradigms

(define power (lambda (b e) (if (= e 1) b (* (power b (- e 1)) b)))) (define stack-copies-of (lambda (quantity image) (if (= quantity 1) image (stack (stack-copies-of (- quantity 1) image) image))))

05 How are the functions different?

slide-12
SLIDE 12

CoSc 450: Programming Paradigms

(define power (lambda (b e) (if (= e 1) b (* (power b (- e 1)) b)))) (define stack-copies-of (lambda (quantity image) (if (= quantity 1) image (stack (stack-copies-of (- quantity 1) image) image))))

05 How are the functions different?

slide-13
SLIDE 13

CoSc 450: Programming Paradigms

(define power (lambda (b e) (if (= e 1) b (* (power b (- e 1)) b)))) (define stack-copies-of (lambda (quantity image) (if (= quantity 1) image (stack (stack-copies-of (- quantity 1) image) image))))

05 How are the functions different? The form is the same. The functions are different.

slide-14
SLIDE 14

CoSc 450: Programming Paradigms

(define together-copies-of (lambda (combine quantity thing) (if (= quantity 1) thing (combine (together-copies-of combine (- quantity 1) thing) thing))))

05

slide-15
SLIDE 15

CoSc 450: Programming Paradigms

(define together-copies-of (lambda (combine quantity thing) (if (= quantity 1) thing (combine (together-copies-of combine (- quantity 1) thing) thing))))

05 The first parameter in the function together-copies-of is a function.

slide-16
SLIDE 16

CoSc 450: Programming Paradigms

(define together-copies-of (lambda (combine quantity thing) (if (= quantity 1) thing (combine (together-copies-of combine (- quantity 1) thing) thing))))

05

(define stack-copies-of (lambda (quantity image) (together-copies-of stack quantity image)))

slide-17
SLIDE 17

CoSc 450: Programming Paradigms

(define together-copies-of (lambda (combine quantity thing) (if (= quantity 1) thing (combine (together-copies-of combine (- quantity 1) thing) thing))))

05

(define stack-copies-of (lambda (quantity image) (together-copies-of stack quantity image)))

Actual parameter

slide-18
SLIDE 18

CoSc 450: Programming Paradigms

(define together-copies-of (lambda (combine quantity thing) (if (= quantity 1) thing (combine (together-copies-of combine (- quantity 1) thing) thing))))

05

(define stack-copies-of (lambda (quantity image) (together-copies-of stack quantity image)))

Formal parameter

slide-19
SLIDE 19

CoSc 450: Programming Paradigms

(define together-copies-of (lambda (combine quantity thing) (if (= quantity 1) thing (combine (together-copies-of combine (- quantity 1) thing) thing))))

05

(define stack-copies-of (lambda (quantity image) (together-copies-of stack quantity image)))

What is the definition of power?

slide-20
SLIDE 20

CoSc 450: Programming Paradigms

(define together-copies-of (lambda (combine quantity thing) (if (= quantity 1) thing (combine (together-copies-of combine (- quantity 1) thing) thing))))

05

(define stack-copies-of (lambda (quantity image) (together-copies-of stack quantity image))) (define power (lambda (base exponent) (together-copies-of * exponent base)))

slide-21
SLIDE 21

CoSc 450: Programming Paradigms

(define num-digits-in-satisfying (lambda (n test?) (cond ((< n 0) (num-digits-in-satisfying (- n) test?)) ((< n 10) (if (test? n) 1 0)) ((test? (remainder n 10)) (+ (num-digits-in-satisfying (quotient n 10) test?) 1)) (else (num-digits-in-satisfying (quotient n 10) test?)))))

05 What is the definition of num-odd-digits?

slide-22
SLIDE 22

CoSc 450: Programming Paradigms

(define num-digits-in-satisfying (lambda (n test?) (cond ((< n 0) (num-digits-in-satisfying (- n) test?)) ((< n 10) (if (test? n) 1 0)) ((test? (remainder n 10)) (+ (num-digits-in-satisfying (quotient n 10) test?) 1)) (else (num-digits-in-satisfying (quotient n 10) test?)))))

05

(define num-odd-digits (lambda (n) (num-digits-in-satisfying n odd?)))

slide-23
SLIDE 23

CoSc 450: Programming Paradigms

(define num-digits-in-satisfying (lambda (n test?) (cond ((< n 0) (num-digits-in-satisfying (- n) test?)) ((< n 10) (if (test? n) 1 0)) ((test? (remainder n 10)) (+ (num-digits-in-satisfying (quotient n 10) test?) 1)) (else (num-digits-in-satisfying (quotient n 10) test?)))))

05

(define num-odd-digits (lambda (n) (num-digits-in-satisfying n odd?)))

What is the definition of num-6s?

slide-24
SLIDE 24

CoSc 450: Programming Paradigms

(define num-digits-in-satisfying (lambda (n test?) (cond ((< n 0) (num-digits-in-satisfying (- n) test?)) ((< n 10) (if (test? n) 1 0)) ((test? (remainder n 10)) (+ (num-digits-in-satisfying (quotient n 10) test?) 1)) (else (num-digits-in-satisfying (quotient n 10) test?)))))

05

(define num-odd-digits (lambda (n) (num-digits-in-satisfying n odd?))) (define num-6s (lambda (n) (num-digits-in-satisfying n (lambda (m) (= m 6)))))

slide-25
SLIDE 25

CoSc 450: Programming Paradigms 05 The Halting Problem

slide-26
SLIDE 26

CoSc 450: Programming Paradigms 05 The Halting Problem Is it possible to write a program that does halt, that can determine whether any other program would halt if it were executed?

slide-27
SLIDE 27

CoSc 450: Programming Paradigms 05 The Halting Problem Is it possible to write a program that does halt, that can determine whether any other program would halt if it were executed? NO!

slide-28
SLIDE 28

CoSc 450: Programming Paradigms 05 The Halting Problem

(define return-seven (lambda () 7))

slide-29
SLIDE 29

CoSc 450: Programming Paradigms 05 The Halting Problem

(define return-seven (lambda () 7)) (define loop-forever (lambda () (loop-forever)))

slide-30
SLIDE 30

CoSc 450: Programming Paradigms 05 The Halting Problem

(define return-seven (lambda () 7)) (define loop-forever (lambda () (loop-forever))) (define halts? (lambda (alpha) #t ; Bug. Should return #t if alpha halts, otherwise #f ))

slide-31
SLIDE 31

CoSc 450: Programming Paradigms 05 The Halting Problem

(define return-seven (lambda () 7)) (define loop-forever (lambda () (loop-forever))) (define halts? (lambda (alpha) #t ; Bug. Should return #t if alpha halts, otherwise #f )) > (halts? return-seven) #t > (halts? loop-forever) #f

slide-32
SLIDE 32

CoSc 450: Programming Paradigms 05 The Halting Problem Proof by contradiction. Assume it is possible to write

halts?, and show that assumption leads to a

contradiction.

slide-33
SLIDE 33

CoSc 450: Programming Paradigms 05 The Halting Problem Proof by contradiction. Assume it is possible to write

halts?, and show that assumption leads to a

contradiction. Construct function debunk-halts?

slide-34
SLIDE 34

CoSc 450: Programming Paradigms 05 The Halting Problem Proof by contradiction. Assume it is possible to write

halts?, and show that assumption leads to a

contradiction. Construct function debunk-halts?

(define debunk-halts? (lambda () (if (halts? debunk-halts?) (loop-forever) (return-seven))))

slide-35
SLIDE 35

CoSc 450: Programming Paradigms 05 There are two possibilities: (a) debunk-halts? halts ⇒ (halts? debunk-halts?) returns #t ⇒ loop-forever? executes ⇒ debunk-halts? does not halt Contradiction

(define debunk-halts? (lambda () (if (halts? debunk-halts?) (loop-forever) (return-seven))))

slide-36
SLIDE 36

CoSc 450: Programming Paradigms 05 There are two possibilities: (a) debunk-halts? halts ⇒ (halts? debunk-halts?) returns #t ⇒ loop-forever? executes ⇒ debunk-halts? does not halt Contradiction

(define debunk-halts? (lambda () (if (halts? debunk-halts?) (loop-forever) (return-seven))))

slide-37
SLIDE 37

CoSc 450: Programming Paradigms 05 There are two possibilities: (a) debunk-halts? halts ⇒ (halts? debunk-halts?) returns #t ⇒ loop-forever? executes ⇒ debunk-halts? does not halt Contradiction

(define debunk-halts? (lambda () (if (halts? debunk-halts?) (loop-forever) (return-seven))))

slide-38
SLIDE 38

CoSc 450: Programming Paradigms 05 There are two possibilities: (a) debunk-halts? halts ⇒ (halts? debunk-halts?) returns #t ⇒ loop-forever executes ⇒ debunk-halts? does not halt Contradiction

(define debunk-halts? (lambda () (if (halts? debunk-halts?) (loop-forever) (return-seven))))

slide-39
SLIDE 39

CoSc 450: Programming Paradigms 05 There are two possibilities: (a) debunk-halts? halts ⇒ (halts? debunk-halts?) returns #t ⇒ loop-forever executes ⇒ debunk-halts? does not halt Contradiction

(define debunk-halts? (lambda () (if (halts? debunk-halts?) (loop-forever) (return-seven))))

slide-40
SLIDE 40

CoSc 450: Programming Paradigms 05 There are two possibilities: (a) debunk-halts? halts ⇒ (halts? debunk-halts?) returns #t ⇒ loop-forever executes ⇒ debunk-halts? does not halt Contradiction

(define debunk-halts? (lambda () (if (halts? debunk-halts?) (loop-forever) (return-seven))))

slide-41
SLIDE 41

CoSc 450: Programming Paradigms 05 There are two possibilities: (b) debunk-halts? does not halt ⇒ (halts? debunk-halts?) returns #f ⇒ return-seven executes ⇒ debunk-halts? does halt Contradiction

(define debunk-halts? (lambda () (if (halts? debunk-halts?) (loop-forever) (return-seven))))

slide-42
SLIDE 42

CoSc 450: Programming Paradigms 05 There are two possibilities: (b) debunk-halts? does not halt ⇒ (halts? debunk-halts?) returns #f ⇒ return-seven executes ⇒ debunk-halts? does halt Contradiction

(define debunk-halts? (lambda () (if (halts? debunk-halts?) (loop-forever) (return-seven))))

slide-43
SLIDE 43

CoSc 450: Programming Paradigms 05 There are two possibilities: (b) debunk-halts? does not halt ⇒ (halts? debunk-halts?) returns #f ⇒ return-seven executes ⇒ debunk-halts? does halt Contradiction

(define debunk-halts? (lambda () (if (halts? debunk-halts?) (loop-forever) (return-seven))))

slide-44
SLIDE 44

CoSc 450: Programming Paradigms 05 There are two possibilities: (b) debunk-halts? does not halt ⇒ (halts? debunk-halts?) returns #f ⇒ return-seven executes ⇒ debunk-halts? does halt Contradiction

(define debunk-halts? (lambda () (if (halts? debunk-halts?) (loop-forever) (return-seven))))

slide-45
SLIDE 45

CoSc 450: Programming Paradigms 05 There are two possibilities: (b) debunk-halts? does not halt ⇒ (halts? debunk-halts?) returns #f ⇒ return-seven executes ⇒ debunk-halts? does halt Contradiction

(define debunk-halts? (lambda () (if (halts? debunk-halts?) (loop-forever) (return-seven))))

slide-46
SLIDE 46

CoSc 450: Programming Paradigms 05 Conclusion: The assumption implies a contradiction in all possible scenarios. Therefore, the assumption is false, and it is impossible to write halts?

(define debunk-halts? (lambda () (if (halts? debunk-halts?) (loop-forever) (return-seven))))

slide-47
SLIDE 47

CoSc 450: Programming Paradigms 05 Procedure factories

> (double 4) 8 > (double 5) 10 > (triple 4) 12 > (triple 5) 15

slide-48
SLIDE 48

CoSc 450: Programming Paradigms 05 Procedure factories

> (double 4) 8 > (double 5) 10 > (triple 4) 12 > (triple 5) 15 (define double (make-multiplier 2)) (define triple (make-multiplier 3))

slide-49
SLIDE 49

CoSc 450: Programming Paradigms 05 Procedure factories

(define double (make-multiplier 2)) (define triple (make-multiplier 3))

Define make-multiplier

slide-50
SLIDE 50

CoSc 450: Programming Paradigms 05 Procedure factories

(define double (make-multiplier 2)) (define triple (make-multiplier 3)) (define make-multiplier (lambda (scaling-factor) (lambda (x) (* x scaling-factor))))

Define make-multiplier

slide-51
SLIDE 51

CoSc 450: Programming Paradigms 05 Procedure factories

(define double (make-multiplier 2)) (define triple (make-multiplier 3)) (define make-multiplier (lambda (scaling-factor) (lambda (x) (* x scaling-factor))))

Define make-multiplier

slide-52
SLIDE 52

CoSc 450: Programming Paradigms 05 Procedure factories

(define double (make-multiplier 2)) (define triple (make-multiplier 3)) (define make-multiplier (lambda (scaling-factor) (lambda (x) (* x scaling-factor))))

Define make-multiplier

slide-53
SLIDE 53

CoSc 450: Programming Paradigms 05 Procedure factories

(define double (make-multiplier 2)) (define triple (make-multiplier 3)) (define make-multiplier (lambda (scaling-factor) (lambda (x) (* x scaling-factor))))

Define make-multiplier

slide-54
SLIDE 54

CoSc 450: Programming Paradigms 05 If the factory manufactures a function that calls itself the function must be named.

(define function-factory (lambda (parameter-for-factory) (define function-returned (lambda (parameter-for-function-returned) ... recursive call to function-returned ... )) function-returned))

slide-55
SLIDE 55

CoSc 450: Programming Paradigms 05

> (repeatedly-square 2 0) 2 > (repeatedly-square 2 1) 4 > (repeatedly-square 2 2) 16 > (repeatedly-square 2 3) 256

slide-56
SLIDE 56

CoSc 450: Programming Paradigms 05

> (repeatedly-square 2 0) 2 > (repeatedly-square 2 1) 4 > (repeatedly-square 2 2) 16 > (repeatedly-square 2 3) 256

Function returned by the factory

(repeatedly-square 2 3)

slide-57
SLIDE 57

CoSc 450: Programming Paradigms 05

> (repeatedly-square 2 0) 2 > (repeatedly-square 2 1) 4 > (repeatedly-square 2 2) 16 > (repeatedly-square 2 3) 256

Function returned by the factory

(repeatedly-square 2 3)

Two parameters

slide-58
SLIDE 58

CoSc 450: Programming Paradigms 05

> (repeatedly-square 2 0) 2 > (repeatedly-square 2 1) 4 > (repeatedly-square 2 2) 16 > (repeatedly-square 2 3) 256

Function returned by the factory

(repeatedly-square 2 3)

Thing on which to operate Two parameters

slide-59
SLIDE 59

CoSc 450: Programming Paradigms 05

> (repeatedly-square 2 0) 2 > (repeatedly-square 2 1) 4 > (repeatedly-square 2 2) 16 > (repeatedly-square 2 3) 256

Function returned by the factory

(repeatedly-square 2 3)

Thing on which to operate How many times to operate Two parameters

slide-60
SLIDE 60

CoSc 450: Programming Paradigms 05

> (repeatedly-square 2 3) 256

Function returned by the factory

(repeatedly-square 2 3)

Calling the factory to make the function

slide-61
SLIDE 61

CoSc 450: Programming Paradigms 05

> (repeatedly-square 2 3) 256

Function returned by the factory

(repeatedly-square 2 3)

Calling the factory to make the function

(define repeatedly-square (make-repeated-version-of sqr))

slide-62
SLIDE 62

CoSc 450: Programming Paradigms 05

> (repeatedly-square 2 3) 256

Function returned by the factory

(repeatedly-square 2 3)

Calling the factory to make the function

(define repeatedly-square (make-repeated-version-of sqr))

Returns a function having two parameters

slide-63
SLIDE 63

CoSc 450: Programming Paradigms 05

> (repeatedly-square 2 3) 256

Function returned by the factory

(repeatedly-square 2 3)

Calling the factory to make the function

(define repeatedly-square (make-repeated-version-of sqr))

Returns a function having two parameters Has only one parameter itself, the operation

slide-64
SLIDE 64

CoSc 450: Programming Paradigms 05

(repeatedly-square 2 3) (define repeatedly-square (make-repeated-version-of sqr))

slide-65
SLIDE 65

CoSc 450: Programming Paradigms 05

(repeatedly-square 2 3) (define repeatedly-square (make-repeated-version-of sqr))

Define the factory

slide-66
SLIDE 66

CoSc 450: Programming Paradigms 05

(repeatedly-square 2 3) (define repeatedly-square (make-repeated-version-of sqr))

(define make-repeated-version-of (lambda (f) ; make a repeated version of f (define the-repeated-version (lambda (b n) ; which does f n times to b (if (= n 0) b (the-repeated-version (f b) (- n 1))))) the-repeated-version))

Define the factory

slide-67
SLIDE 67

CoSc 450: Programming Paradigms 05

(repeatedly-square 2 3) (define repeatedly-square (make-repeated-version-of sqr))

(define make-repeated-version-of (lambda (f) ; make a repeated version of f (define the-repeated-version (lambda (b n) ; which does f n times to b (if (= n 0) b (the-repeated-version (f b) (- n 1))))) the-repeated-version))

Define the factory

slide-68
SLIDE 68

CoSc 450: Programming Paradigms 05

(repeatedly-square 2 3) (define repeatedly-square (make-repeated-version-of sqr))

(define make-repeated-version-of (lambda (f) ; make a repeated version of f (define the-repeated-version (lambda (b n) ; which does f n times to b (if (= n 0) b (the-repeated-version (f b) (- n 1))))) the-repeated-version))

Define the factory

slide-69
SLIDE 69

CoSc 450: Programming Paradigms 05

(repeatedly-square 2 3) (define repeatedly-square (make-repeated-version-of sqr))

(define make-repeated-version-of (lambda (f) ; make a repeated version of f (define the-repeated-version (lambda (b n) ; which does f n times to b (if (= n 0) b (the-repeated-version (f b) (- n 1))))) the-repeated-version))

Define the factory

slide-70
SLIDE 70

CoSc 450: Programming Paradigms 05

(repeatedly-square 2 3) (define repeatedly-square (make-repeated-version-of sqr))

(define make-repeated-version-of (lambda (f) ; make a repeated version of f (define the-repeated-version (lambda (b n) ; which does f n times to b (if (= n 0) b (the-repeated-version (f b) (- n 1))))) the-repeated-version))

Define the factory

slide-71
SLIDE 71

CoSc 450: Programming Paradigms 05

(repeatedly-square 2 3) (define repeatedly-square (make-repeated-version-of sqr))

(define make-repeated-version-of (lambda (f) ; make a repeated version of f (define the-repeated-version (lambda (b n) ; which does f n times to b (if (= n 0) b (the-repeated-version (f b) (- n 1))))) the-repeated-version))

Define the factory

slide-72
SLIDE 72

CoSc 450: Programming Paradigms 05

(repeatedly-square 2 3) (define repeatedly-square (make-repeated-version-of sqr))

(define make-repeated-version-of (lambda (f) ; make a repeated version of f (define the-repeated-version (lambda (b n) ; which does f n times to b (if (= n 0) b (the-repeated-version (f b) (- n 1))))) the-repeated-version))

Define the factory

slide-73
SLIDE 73

CoSc 450: Programming Paradigms 05

(repeatedly-square 2 3) (define repeatedly-square (make-repeated-version-of sqr))

(define make-repeated-version-of (lambda (f) ; make a repeated version of f (define the-repeated-version (lambda (b n) ; which does f n times to b (if (= n 0) b (the-repeated-version (f b) (- n 1))))) the-repeated-version))

Define the factory