Procedural Abstraction Topic 5.5 • We have seen the use of procedures as abstractions. • So far we have defined cases where the abstractions Higher Order Procedures that are captured are essentially compound (This goes back and picks up section operations on numbers. • What does that buy us? 1.3 and then sections in Chapter 2) – Assign a name to a common pattern (e.g., cube) and then we can work with the abstraction instead of the individual operations. • What more could we do? – What about the ability to capture higher-level “programming” September 2008 patterns. – For this we need procedures are arguments/return values from procedures Spring 2008 Programming Development 1 Spring 2008 Programming Development 2 Techniques Techniques The really big idea Section 1.3 -Terminology • Procedures that accept other procedures as input or • Procedures (function) should be treated as first- return a procedure as output are higher-order class objects procedures. • In scheme procedures (functions) are data • The other procedures are first-order procedures. – can be passed to other procedures as arguments – can be created inside procedures – can be returned from procedures • Scheme treats functions/ procedures as first- • This notion provides big increase in abstractive power class objects. They can be manipulated like • One thing that sets scheme apart from most other any other object. programming languages Spring 2008 Programming Development 3 Spring 2008 Programming Development 4 Techniques Techniques Book and Here… In mathematics… • Book goes through showing several examples of the • Not all operations take in (only) numbers abstract pattern of summation, and then shows how you might want to abstract that into a procedure. • CAUTION: I find some of the names that they use for • +, -, *, /, expt, log, mod, … their abstraction confusing – don’t let that bother you! – take in numbers, return numbers It just makes reading the book a little more difficult. • I am going to borrow an introduction from some old • but operations like Σ , d/dx, integration slides from Cal-Tech. I think you should be able to – take in functions put the two together very nicely. – return functions (or numbers) • At least, that’s my intention… Spring 2008 Programming Development 5 Spring 2008 Programming Development 6 Techniques Techniques 1
Math: Math: Functions as Arguments Functions as Arguments • You’ve seen: • Σ is a “function” 6 ∑ – which takes in f ( x ) 6 • a function ∑ • a lower bound (an integer) x = 0 a = f ( n ) • an upper bound (also an integer) – and returns • a number n = 0 • We say that Σ is a “higher-order” function a=f(0)+f(1)+f(2)+f(3)+f(4)+f(5)+f(6) • Can define higher-order fns in scheme Spring 2008 Programming Development 7 Spring 2008 Programming Development 8 Techniques Techniques Transforming summation Summation in scheme high ∑ high f ( low ) + ( f ( x )) ∑ f ( x ) x = low + 1 x = low ; takes a function a low value and a high value is the same as… ; returns the sum of f(low)...f(high) by incrementing ; by 1 each time high ∑ (define (sum f low high) f ( low ) + f ( x ) (if (> low high) 0 (+ (f low) x = low + 1 (sum f (+ low 1) high)))) Spring 2008 Programming Development 9 Spring 2008 Programming Development 10 Techniques Techniques Evaluating summation …continuing evaluation • Evaluate: (sum square 2 4) • (if (> 2 4) 0 • ((lambda (f low high) …) square 2 4) (+ (square 2) (sum square 3 4))) • substitute: • (+ (square 2) (sum square 3 4))) – square for f • (square 2) … 4 – 2 for low, 4 for high • (+ 4 (sum square 3 4))) Spring 2008 Programming Development 11 Spring 2008 Programming Development 12 Techniques Techniques 2
…continuing evaluation …continuing evaluation • (+ 4 (+ 9 (sum square 4 4)))) • (+ 4 (sum square 3 4))) • yadda yadda… • (+ 4 (if (> 3 4) 0 • (+ 4 (+ 9 (+ 16 (sum square 5 4)))) (+ (square 3) • (+ 4 (+ 9 (+ 16 (if (> 5 4) 0 …) (sum square 4 4)))) • (+ 4 (+ 9 (+ 16 0))) • (+ 4 (+ (square 3) • … 29 (whew!) (sum square 4 4)))) • pop quiz: what kind of process? • (+ 4 (+ 9 (sum square 4 4)))) – linear recursive Spring 2008 Programming Development 13 Spring 2008 Programming Development 14 Techniques Techniques Also valid… Iterative version (sum (lambda (x) (* x x)) 2 4) • sum generates a recursive process – this is also a valid call • iterative process would use less space – equivalent in this case – no pending operations – no need to give the function a name • Can we re-write to get an iterative version? Spring 2008 Programming Development 15 Spring 2008 Programming Development 16 Techniques Techniques Iterative version Evaluating iterative version • (isum square 2 4) ; takes a function a low value and a high value • (sum-iter square 2 4 0) ; returns the sum of f(low)...f(high) by incrementing • (if (> 2 4) 0 ; by 1 each time (sum-iter square (+ 2 1) 4 (+ (square 2) 0))) (define (isum f low high) • (sum-iter square (+ 2 1) 4 (+ (square 2) 0)) • (sum-iter square 3 4 (+ 4 0)) (sum-iter f low high 0)) • (sum-iter square 3 4 4) (define (sum-iter f low high result) (if (> low high) result (sum-iter f (+ low 1) high (+ (f low) result)))) Spring 2008 Programming Development 17 Spring 2008 Programming Development 18 Techniques Techniques 3
eval iterative sum cont’d... eval iterative sum cont’d... • (sum-iter square 3 4 4) • (sum-iter square 4 4 13) • (if (> 3 4) 4 • (if (> 4 4) 13 (sum-iter square (+ 3 1) 4 (+ (square 3) 4))) (sum-iter square (+ 4 1) 4 (+ (square 4) 13))) • (sum-iter square (+ 3 1) 4 (+ (square 3) 4)) • (sum-iter square (+ 4 1) 4 (+ (square 4) 13)) • (sum-iter square 4 4 (+ 9 4)) • (sum-iter square 5 4 (+ 16 13)) • (sum-iter square 4 4 13) • (sum-iter square 5 4 29) Spring 2008 Programming Development 19 Spring 2008 Programming Development 20 Techniques Techniques eval iterative sum cont’d... recursive vs. iterative (define (isum f low high) (define (sum f low high) • (sum-iter square 5 4 29) (define (sum-iter f low high result) (if (> low high) • (if (> 5 4) 29 (sum-iter ...)) 0 (if (> low high) (+ (f low) result • 29 (sum f (sum-iter f • same result, no pending operations (+ low 1) high)))) (+ low 1) high • more space-efficient (+ (f low) result)))) (sum-iter f a b 0)) Spring 2008 Programming Development 21 Spring 2008 Programming Development 22 Techniques Techniques recursive vs. iterative Historical interlude Reactions on first seeing “lambda”: • recursive: – What the heck is this thing? – pending computations – What the heck is it good for? – when recursive calls return, still work to do – Where the heck does it come from? • iterative: – current state of computation stored in operands of internal procedure This represents the essence of a function – no need to give it a name. It comes from mathematics. Where ever you might – when recursive calls return, no more work to do (“tail recursive”) use the name of a procedure – you could use a lambda expression and not bother to give the procedure a name. Spring 2008 Programming Development 23 Spring 2008 Programming Development 24 Techniques Techniques 4
Generalizing summation stepping by 1, 2, ... ; takes a number and increments it by 1 • What if we don’t want to go up by 1? (define (step1 n) (+ n 1)) • Supply another procedure – given current value, finds the next one ; new definition of sum... (define (new-sum f low high) ; same as before ; takes a function, a low value, a function to generate the (gsum f low step1 high)) next ; value and the high value. Returns f(low)...f(high) by ; takes a number and increments it by 2 (define (step2 n) (+ n 2)) ; incrementing according to next each time (define (gsum f low next high) ; new definition of a summation that goes up by 2 each time (if (> low high) 0 (define (sum2 f low high) (+ (f low) (gsum f low step2 high)) (gsum f (next low) next high)))) Spring 2008 Programming Development 25 Spring 2008 Programming Development 26 Techniques Techniques stepping by 2 using lambda • (define (step2 n) (+ n 2)) • (sum square 2 4) = 2 2 + 3 2 + 4 2 • (define (sum2 f low high) (gsum f low step2 high)) • (sum2 square 2 4) = 2 2 + 4 2 • Why not just write this as: • (sum2 (lambda (n) (* n n n)) 1 10) • (define (sum2 f low high) = 1 3 + 3 3 + 5 3 + 7 3 + 9 3 (gsum f low (lambda (n) (+ n 2)) high)) • don’t need to name tiny one-shot functions Spring 2008 Programming Development 27 Spring 2008 Programming Development 28 Techniques Techniques (ab)using lambda Big Ideas • Procedures (functions) are data! • How about: – sum of n 4 for n = 1 to 100, stepping by 5? • We can abstract operations around functions as well as numbers • (gsum (lambda (n) (* n n n n)) • Provides great power 1 – expression, abstraction (lambda (n) (+ n 5)) – high-level formulation of techniques 100) • We’ve only scratched the surface! • NOTE: the n’s in the lambdas are independent of each other Spring 2008 Programming Development 29 Spring 2008 Programming Development 30 Techniques Techniques 5
Recommend
More recommend