Skip to main content

Lec 3: higher-order procedures 1

Big ideas

  1. Functions as data
  2. First class data types

Our brain: We know how to do things (verbs) and we know what they are (noun).

Computers: data (numbers and character strings etc.) procedures (sets of instructions)

Our human minds and the languages we use are setup to distinguish between verbs and nouns. But in computing we need to break down the barrier. Data and procedures are closely related.

An integral is a function of functions. It takes a function and gives you a different function. So it's a function who's argument is a function.

Generalizing Patterns

Example:

(define pi 3.141592654)

(define (square-area r) (* r r))

(define (circle-area r) (* pi r r))

(define (sphere-area r) (* 4 pi r r))

(define (hexagon-area r) (* (sqrt 3) 1.5 r r))

This is a list of procedures. But what is the common pattern and can we generalize?

; generalized pattern

(define (shape-area f r) (* f r r))

(define (circle-area-new r) (shape-area pi r))

(circle-area-new 10)

; Out lord and saviour Brian Harvey's pattern (which is obviously nicer than mine)

(define (area shape r) (* shape r r))

(define circle pi)

(area circle 10)

Another example

(define (sumsquare a b)
(if (> a b)
0
(+ (* a a) (sumsquare (+ a 1) b))))

(define (cubesquare a b)
(if (> a b)
0
(+ (* a a a) (cubesquare (+ a 1) b))))

(sumsquare 3 5)

(cubesquare 3 5)
50
216
; generalized pattern

(define (sum term a b)
(if (> a b)
0
(+ (term a) (sum term (+ a 1) b))))

(define (square n) (* n n))

(define (cube n) (* n n n))

(sum square 3 5)

(sum cube 3 5)
50
216

Note: Brian says that case sensitive languages are not sensible. If you have "Foo", "fOo" and "foO" in one function then you are "asking for trouble". I think this makes sense, It wouldn't stop you from using capitalized casing like camel case.

The difference between (square) and square is the invocation of the function square. The parenthesis indicated that the procedure is being run, whereas the lack of parenthesis means that the procedure is passed as data and is not run. It is evaluated as a function. The data is a function.

Tangent

The two most important words in the english language are domain and range.

Domain of a function is what kinds of things does it take as arguments.

Range what kinds of things does it return as a result.

If a function returns a sentence, but there are no values then you will need to return an empty sentence. It range is a sentence.

Example using sentences which have a common pattern

; function which takes a sentence of numbers and returns only the evens

(define (evens nums)
(cond ((empty? nums) '())
((equal? (remainder (first nums) 2) 0) (se (first nums) (evens (bf nums))))
(else (evens (bf nums)))))

(evens '(2 4 5 6 7 8 9 1))
; function which takes a sentence of words and returns the subset of those which have the letter e

(define (ewords words)
(cond ((empty? words) '())
((member? 'e (first words)) (se (first words) (ewords (bf words))))
(else (ewords (bf words)))))

(ewords '(got to get you into my life))

Generalized form of the above two sentence.

; generalized form

(define (sentence-property check? sent)
(cond ((empty? sent) '())
((check? (first sent)) (se (first sent) (sentence-property check? (bf sent))))
(else (sentence-property check? (bf sent)))))

(define (evens-new nums)
(sentence-property (lambda (x) (equal? (remainder x 2) 0)) nums))

(evens-new '(1 2 3 4 5 6 7 8 9))

(define (ewords-new words)
(sentence-property (lambda (wd) (member? 'e wd)) words))

(ewords-new '(got to get you into my life))

Alonzo Church and Lambda Functions

Alonzo Church invented a formalism to talk about mathematical ideas based on the notion of functions. He used the greek letter lambda to represent the thing which makes a function

Alonzo's notation would look something like:

λx.body\lambda x . body

Which is basically how we formulate lambda functions in Scheme.

Lambda's and definitions of procedures are the same thing.

(define (foo x) (body))

Is just shorthand for...

(define foo (lambda (x) (body)))