cs3 fall 2005 lecture 10: more on higher order functions

15
CS3 Fall 2005 Lecture 10: More on higher order functions

Post on 19-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS3 Fall 2005 Lecture 10: More on higher order functions

CS3 Fall 2005

Lecture 10: More on higher order functions

Page 2: CS3 Fall 2005 Lecture 10: More on higher order functions

Oct 24 Higher order procedures

Oct 31 More HOF – "tic-tac-toe" program: SS chapter 10!– "Change Making" case study, in the reader

Nov 7 Miniproject #3: Election-processing– Friday is a holiday– Thursday a catch-up day– MP#3 due Thur/Fri: plan ahead!

Review Session: Wednesday

Nov 14 Midterm #2

Lists

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

Nov 28 Work on the project: checks #2

Dec 5 Checkoff #3, finish the project

Dec 17 Final Exam (midterm #3)

Page 3: CS3 Fall 2005 Lecture 10: More on higher order functions

Administrivia• Reading is important for this week:

– Tic-tac-toe, chapter 10 in Simply Scheme (Tuesday)– Difference between dates part III case-study in the

reader (Tuesday)– Change-making case-study in the reader (Thursday)

• Note: there is a limit to the number of quizzes you can take outside of lab: 4 quizzes

• Make sure you have completed theMid Semester Survey

Page 4: CS3 Fall 2005 Lecture 10: More on higher order functions

Tic Tac Toe

Page 5: CS3 Fall 2005 Lecture 10: More on higher order functions

X | | ---+---+--- O | O | X ---+---+--- | |

"X _ _" "O O X" "_ _ _"

"X _ _ O O X _ _ _"

The board

Page 6: CS3 Fall 2005 Lecture 10: More on higher order functions

X | | ---+---+--- O | O | X ---+---+--- | |

"X _ _ O O X _ _ _"

Triples (another representation to help the search for possible moves)

( )x23 oox 789 xo7 2o8 3x9 xo9 3o7

Page 7: CS3 Fall 2005 Lecture 10: More on higher order functions

Higher order function (HOFs)

• A HOF is a procedure that takes a procedure 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 8: CS3 Fall 2005 Lecture 10: More on higher order functions

A definition of every

(define (my-every proc ws)

(if (empty? ws)

'()

(se (proc (first ws))

(my-every (bf ws))

)))

Every does a lot of work for you:• Checking the conditional• Returning the proper base case• Combing the various recursive steps• Invoking itself recursively on a smaller problem

Page 9: CS3 Fall 2005 Lecture 10: More on higher order functions

Which HOFs would you use to write these?

1) capitalize-proper-names(c-p-n '(mr. smith goes to washington))

(mr. Smith goes to Washington)

2) count-if(count-if odd? '(1 2 3 4 5)) 3

3) longest-word(longest-word '(I had fun on spring break)) spring

4) count-vowels-in-each(c-e-l '(I have forgotten everything)) (1 2 3 3)

5) squares-greater-than-100(s-g-t-100 '(2 9 13 16 9 45) (169 256 2025)

6) sum-of-squares(sos '(1 2 3 4 5 6 7) 30

7) successive-concatenation(sc '(a b c d e) (a ab abc abcd abcde)

Page 10: CS3 Fall 2005 Lecture 10: More on higher order functions

Write successive-concatenation

(sc '(a b c d e)) (a ab abc abcd abcde)

(sc '(the big red barn)) (the thebig thebigred thebigredbarn)

(define (sc sent) (accumulate (lambda ?? ) sent))

Page 11: CS3 Fall 2005 Lecture 10: More on higher order functions

HOF: Base cases can be confusing

• What does (every square '()) return?– (every square "")– (every square "12345")

• What about (keep odd? '())– (keep odd? "")

• How about (accumulate + '())– (accumulate + "")– (accumulate * '())– (accumulate word '(a))– (accumulate + '(a)– (accumulate word '(a b))– (accumulate + '(a b))

Page 12: CS3 Fall 2005 Lecture 10: More on higher order functions

lambda

• "lambda" is a special form that returns a function:

(lambda (param1 param2 …)statement1statement2

)

(lambda (x) (* x x) [a function]

(every (lambda (x) (* x x) '(1 2 3 4)) (1 4 9 16)

Page 13: CS3 Fall 2005 Lecture 10: More on higher order functions

Can a function defined by a lambda be recursive?

(lambda (sent)

(if (empty? sent)

'()

(se (square (first sent))

(???? (bf sent)))))

Page 14: CS3 Fall 2005 Lecture 10: More on higher order functions

When do you NEED lambda?

1. When you need the context (inside a two-parameter procedure)

(add-suffix '-is-great '(nate sam mary)) (nate-is-great sam-is-great mary-is-great)

2. When you need to make a function on the fly

Page 15: CS3 Fall 2005 Lecture 10: More on higher order functions

Procedures that make procedures

• Generally, name procedures that create procedures "make-XXX"

(make-bookends 'o) #[closure arglist=(inner-wd) d7d0e0]

((make-bookends 'o) 'hi) ohio

((make-bookends 'to) 'ron) toronto

(define tom-bookend (make-bookends 'tom))

(tom-bookends "") tomtom