slide 1 cs3 fall 2005 lecture 9: higher order functions

16
Slide 1 CS3 Fall 2005 Lecture 9: Higher Order Functions

Post on 21-Dec-2015

216 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Slide 1 CS3 Fall 2005 Lecture 9: Higher Order Functions

Slide 1

CS3 Fall 2005

Lecture 9: Higher Order Functions

Page 2: Slide 1 CS3 Fall 2005 Lecture 9: Higher Order Functions

Slide 2

Oct 17 Miniproject #2: Number-spelling

Oct 24 Higher order procedures

Oct 31 More HOF, the "tic-tac-toe" program

"Change Making" case study

Nov 7 Miniproject #3: Election-processing

Nov 14 Midterm #2

Lists

Nov 21 Start on the project: check-off #1

Nov 28 Work on the project: checks #2 and #3

Dec 5 Finish and turn in the project

Dec 17 Final Exam (midterm #3)

Page 3: Slide 1 CS3 Fall 2005 Lecture 9: Higher Order Functions

Slide 3

Administrivia

• Mid-semester survey this week (Thur/Fri) – you NEED to do this.

• Reading this week: – Simply Scheme Chapters 7-9– Difference between dates, part III

• Two resources:– http://hiroki.ucdev.org/cs3fall05– http://wla.berkeley.edu/

Page 4: Slide 1 CS3 Fall 2005 Lecture 9: Higher Order Functions

Slide 4

When should the Midterm #2 review be?

The second midterm is on Monday, Nov. 14

Do you want the review on:

Sunday? (Nov. 13)

Fri night? (Nov. 11)

Wed night? (Nov. 9)

And, what time?

Page 5: Slide 1 CS3 Fall 2005 Lecture 9: Higher Order Functions

Slide 5

Quick review from last time: binary

• Write binary, a procedure to generate the possible binary numbers given n bits.

(binary 1)(0 1)

(binary 2)(00 01 10 11)

(binary 3)(000 001 010 011 100 101 110 111)

Page 6: Slide 1 CS3 Fall 2005 Lecture 9: Higher Order Functions

Slide 6

What is a procedure?

(or, a function).

Page 7: Slide 1 CS3 Fall 2005 Lecture 9: Higher Order Functions

Slide 7

Treating functions as things

• “define” associates a name with a value– The usual form associates a name with a object that

is a function (define (square x) (* x x)) (define (pi) 3.1415926535)

– You can define other objects, though: (define *pi* 3.1415926535) (define *month-names* ‘(january february march april may june july august september october november december))

Page 8: Slide 1 CS3 Fall 2005 Lecture 9: Higher Order Functions

Slide 8

Consider two forms of “month-name”:

(define (month-name1 date) (first date))

(define month-name2 first)

Page 9: Slide 1 CS3 Fall 2005 Lecture 9: Higher Order Functions

Slide 9

Why have procedures as objects?

Other programming languages don’t (often)

Page 10: Slide 1 CS3 Fall 2005 Lecture 9: Higher Order Functions

Slide 10

Procedures can be takenas arguments…

(define (apply-to-5-and-3 func) (func 5 3))

(define (math-function? func) (or (equal? func +) (equal? func -) (equal? func *) (equal? func /)))

Page 11: Slide 1 CS3 Fall 2005 Lecture 9: Higher Order Functions

Slide 11

…and procedures can be returned from procedures

(define (choose-func name)

(cond ((equal? name ‘plus) +)

((equal? name ‘minus) -)

((equal? name ‘divide) /)

(else ‘sorry)))

(define (make-add-to number)

(lambda (x) (+ number x)))

(define add-to-5 (make-add-to 5))

Page 12: Slide 1 CS3 Fall 2005 Lecture 9: Higher Order Functions

Slide 12

Higher order function (HOFs)

• A HOF is a function that takes a function as an argument.

• There are three main ones that work with words and sentences:– every – do something to each element– keep – return only certain elements– accumulate – combine the elements

Page 13: Slide 1 CS3 Fall 2005 Lecture 9: Higher Order Functions

Slide 13

• Most recursive functions that operate on a sentence fall into:– Mapping: square-all EVERY

– Counting: count-vowels, count-evens– Finding: member, first-even– Filtering: keep-evens KEEP

– Testing: all-even?– Combining: sum-evens ACCUMULATE

Patterns for simple recursions

• Most recursive functions that operate on a sentence fall into:– Mapping: square-all– Counting: count-vowels, count-evens– Finding: member, first-even– Filtering: keep-evens– Testing: all-even?– Combining: sum-evens

Page 14: Slide 1 CS3 Fall 2005 Lecture 9: Higher Order Functions

Slide 14

(define (square-all sent)

(if (empty? sent)

‘()

(se (square (first sent))

(square-all (bf sent))

))

(square-all ‘(1 2 3 4 5))

(every square ‘(1 2 3 4 5))

Page 15: Slide 1 CS3 Fall 2005 Lecture 9: Higher Order Functions

Slide 15

Write "my-every"

(my-every factorial '(1 2 3 4 5)) (1 2 6 24 120)

Page 16: Slide 1 CS3 Fall 2005 Lecture 9: Higher Order Functions

Slide 16

Write "my-keep"

(my-keep odd? '(1 2 3 4 5)) (1 3 5)