higher order procedures cosc 450 programming paradigms 05
play

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,


  1. CoSc 450: Programming Paradigms 05 Higher-Order Procedures

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

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

  4. CoSc 450: Programming Paradigms 05 (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))))

  5. CoSc 450: Programming Paradigms 05 How are the functions similar? (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))))

  6. CoSc 450: Programming Paradigms 05 How are the functions similar? (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))))

  7. CoSc 450: Programming Paradigms 05 How are the functions similar? (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))))

  8. CoSc 450: Programming Paradigms 05 How are the functions similar? (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))))

  9. CoSc 450: Programming Paradigms 05 How are the functions similar? (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))))

  10. CoSc 450: Programming Paradigms 05 How are the functions similar? (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))))

  11. CoSc 450: Programming Paradigms 05 How are the functions different? (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))))

  12. CoSc 450: Programming Paradigms 05 How are the functions different? (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))))

  13. CoSc 450: Programming Paradigms 05 How are the functions different? (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)))) The form is the same. The functions are different.

  14. CoSc 450: Programming Paradigms 05 (define together-copies-of (lambda (combine quantity thing) (if (= quantity 1) thing (combine (together-copies-of combine (- quantity 1) thing) thing))))

  15. CoSc 450: Programming Paradigms 05 (define together-copies-of (lambda (combine quantity thing) (if (= quantity 1) thing (combine (together-copies-of combine (- quantity 1) thing) thing)))) The first parameter in the function together-copies-of is a function.

  16. CoSc 450: Programming Paradigms 05 (define together-copies-of (lambda (combine quantity thing) (if (= quantity 1) thing (combine (together-copies-of combine (- quantity 1) thing) thing)))) (define stack-copies-of (lambda (quantity image) (together-copies-of stack quantity image)))

  17. CoSc 450: Programming Paradigms 05 (define together-copies-of (lambda (combine quantity thing) (if (= quantity 1) thing (combine (together-copies-of combine (- quantity 1) thing) thing)))) (define stack-copies-of (lambda (quantity image) (together-copies-of stack quantity image))) Actual parameter

  18. CoSc 450: Programming Paradigms 05 (define together-copies-of (lambda (combine quantity thing) (if (= quantity 1) thing (combine (together-copies-of combine (- quantity 1) thing) thing)))) (define stack-copies-of (lambda (quantity image) (together-copies-of stack quantity image))) Formal parameter

  19. CoSc 450: Programming Paradigms 05 (define together-copies-of (lambda (combine quantity thing) (if (= quantity 1) thing (combine (together-copies-of combine (- quantity 1) thing) thing)))) (define stack-copies-of (lambda (quantity image) (together-copies-of stack quantity image))) What is the definition of power ?

  20. CoSc 450: Programming Paradigms 05 (define together-copies-of (lambda (combine quantity thing) (if (= quantity 1) thing (combine (together-copies-of combine (- quantity 1) thing) thing)))) (define stack-copies-of (lambda (quantity image) (together-copies-of stack quantity image))) (define power (lambda (base exponent) (together-copies-of * exponent base)))

  21. CoSc 450: Programming Paradigms 05 (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?))))) What is the definition of num-odd-digits ?

  22. CoSc 450: Programming Paradigms 05 (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?))))) (define num-odd-digits (lambda (n) (num-digits-in-satisfying n odd?)))

  23. CoSc 450: Programming Paradigms 05 (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?))))) (define num-odd-digits (lambda (n) (num-digits-in-satisfying n odd?))) What is the definition of num-6s ?

  24. CoSc 450: Programming Paradigms 05 (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?))))) (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)))))

  25. CoSc 450: Programming Paradigms 05 The Halting Problem

  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?

  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!

  28. CoSc 450: Programming Paradigms 05 The Halting Problem (define return-seven (lambda () 7))

  29. CoSc 450: Programming Paradigms 05 The Halting Problem (define return-seven (lambda () 7)) (define loop-forever (lambda () (loop-forever)))

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

  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

  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.

  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?

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

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

Recommend


More recommend