Announcements:

 

Today: Higher Order Procedures:

This is a PROGRAMMING PARADIGM:

See if you really grok functions and the RSM. Everything today follows from the RSM!

> Serious mind bender day <

Today's mantra: PROCEDURES ARE LIKE ANY OTHER VALUES IN SCHEME

 

 

One of the biggest tools in programming is ABSTRACTION:

More precisely:

 

So we saw things like taking (* 3 3), (* 4 4), etc and writing:

(define (square x) (lambda (x) (* x x)))

number --> [square] --> number

And the "what is" description that (square x)=x^2

In some sense this captures the process of squaring.

Now, let's take this a step further.

Sigma = summation.

n

Sigma i^2 = n (n+1) (2n+1) / 6 <--- nice little induction

i=1

Sigma is an operation:

function, 2 numbers ---> [Sigma] ---> number

In mathematics, this saves a good bit of work. In programming, too.

Say we want to write a program for SUM(i=a to b) i

(define sum-int
  (lambda (a b)
    (if (> a b)
	0
	(+ a (sum-int (+ a 1) b)))))

Notes:

Similarly for SUM(i=a to b) i^2

(define sum-squares
  (lambda ( a b)
    (if (> a b)
	0
	(+ (square a) (sum-squares (+ a 1) b)))))

These share a LOT of structure.

So, let's ABSTRACT:

numbers rather than just integers

(define sum
  (lambda (a b thing-to-sum step)
    (if (> a b)
	0
	(+ (thing-to-sum a)
	   (sum (step a) b thing-to-sum step)))))

Note: this is a lot like problem 1 in problem set 1.

thing-to-sum is a PROCEDURE,

(sum 1 2 square one-plus) ==> 1^2 + 2^2 = 5

where we've done:

(define (one-plus x)  (+ x 1)))

"evaluation steps" of substitution model:

(sum 1 2 square one-plus)
==>
(+ (square 1) (sum 2 2 square one-plus))
==>
(+ (square 1) (+ (square 2) (sum 3 2 square one-plus)))
==>
(+ (square 1) (+ (square 2) 0))
==>
(+ 1 (+ 4 0))
==>
5

 

Now, we can define sum-squares:

(define (sum-squares a b)
  (sum a b square one-plus))

We can actually make sum do numeric integration:

Integral(a,b) f

is approximately

(f(a) + f(a+delta x) + ... + f(b)) * delta x

(define (integral f a b dx)
  (* dx
     (sum a b f (lambda (x) (+ x dx)))))

^^^ Creates a procedure which increments by dx.

No point to giving it a name, it's a throwaway.

We could have named this internal function using LET or LETREC if we wanted to, but no real point. Not every value needs a name -- same with lambdas

Now we can use it:

arctan(a) = Integral(0,a) (1/(1+x^2)) dx

(define (arctan a)
  (integral (lambda (x) (/ 1 (+ (square x) 1)))
	    0 a 0.001))

THE POINT:


Okay, so just saw passing functions as arguments to functions.

THE NEXT STEP: RETURNING functions as values of functions.

Note: this is generally harder for students to understand than the above. But again it all follows from the RSM!

(define add-n
  (lambda (n)
    (lambda (x)
      (+ x n))))

Note: this is a procedure which returns a procedure.

{proc(n) (lambda (x) (+ x n))}

The value of (add-n 2) is a procedure that adds 2 to its argument

(add-n 2)

--> {proc (x) (+ {2} x)}

^^^ Notice that this {2} has been evaluated and stuck in there.

And you use it:

(define add2  (add-n 2)) What is add2??? A FUNCTION!

 

(add2 10)
[{Proc(x)(+ x {2})} {10}]
(+ {10} {2})
{12}

 

Remember, mantra: PROCEDURES ARE LIKE ANY OTHER VALUES IN SCHEME


Derivatives:

D (x^3) = 3x^2

What's D? It's an operation which takes a function (like x^3), and

returns another one (like 3x^2).

func --> D --> func

We can approximate D f(x) as

f(x+dx)-f(x)

------------

dx

(define deriv 
  (lambda (f dx)
    (lambda (x)
      (/ (- (f (+ x dx))
	    (f x))
	 dx))))

So, calling (deriv f dx) returns another procedure.

((deriv cube 0.001) 5) ==> 75.015

^^^^^ See CS 222 or 421 for what to do about this sort of inaccuracy in numerical approx.

Look at how deriv is applied:

1. deriv returns a PROCEDURE

2. So, it is used as the operation, first position of a combination


Some other interesting stuff with higher order procedures:

((repeated f n) x) ==> apply F N times to X.

= f(f(...(f(x))...))

func, numb --> repeated --> func

 

(define repeated 
  (lambda (f n)
    (lambda (x)
      (if (= n 0)
	  x
	  ((repeated f (- n 1))
	   (f x))))))

((repeated square 3) X) is (square (square (square X))),

or eighth-power:

((repeated square 3) 2) ==> 256

So (repeated f n) is a procedure that does f n times.


Procedures in Scheme are "first-class objects":

We've seen the first three so far.