Download - Administrivia

Transcript
Page 1: Administrivia
Page 2: Administrivia

Administrivia

• Make sure you have completed theMid Semester Survey– Scheduled the week before Spring Break– this will be worth some points

• Reading is important for this week:– Tic-tac-toe, chapter 10 in SS– Case study: change-making

Page 3: Administrivia

March 14 Higher order procedures

March 21 Spring Break

March 28 Tic-Tac-Toe – read SS chapter 10!

Case Study: Making Change (tree-recursion) – in the reader!

April 4 Elections mini-project (#3)

April 11 Midterm #2 (during lecture)

Extending sentences, words to lists (in lab)

April 18 Advanced list processing (recursion)

Start on the big project

April 25 Work on the big project (check-off 1, 2)

May 2 Big project (check-off 3)

May 9 Big Project due!

Page 4: Administrivia

Tic Tac Toe

Page 5: Administrivia

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 6: Administrivia

(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 7: Administrivia

Write "my-keep"

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

Page 8: Administrivia

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 9: Administrivia

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 10: Administrivia

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 11: Administrivia

lambda

• "lambda" is a special form that lets you make 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 12: Administrivia

Can a function defined by a lambda be recursive?

(lambda (sent)

(if (empty? sent)

'()

(se (square (first sent))

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

Page 13: Administrivia

When do you NEED lambda?

1. When you need the context

(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 14: Administrivia

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

Page 15: Administrivia

Ha!

• Make a procedure that returns a procedure that takes a number X and returns a procedure that takes a number and adds X to it.

– Whew!