Slide 1

advertisement
Slide 1
Administrivia
• Mid-semester survey this week (Thur/Fri)
– you NEED to do this.
• Interviews should be starting in a few
weeks.
• I apologize for not getting the midterm
solutions and last week's lecture on-line:
check tomorrow.
Slide 2
March 7
Tree recursion, etc.
Number-spelling Miniproject (#2)
March 14 Higher order procedures
March 21 Spring Break
March 28 More HOF, tic-tac-toe
Elections Miniproject (#3)
April 4
Case Study: Making Change (treerecursion)
April 11 Midterm #2 (during lecture)
Extending sentences, words to lists (in lab)
April 18 Advanced list processing (recursion)
Start on the big project
Slide 3
> (find-evens '(2 3 4 5 6))
sent = ( 2 3 4 5 6 )
(se 2
sent = ( 3 4 5 6 )
sent = ( 4 5 6 )
(se 4
sent = ( 5 6 )
sent = ( 6 )
(se 6
sent = ( )
()
 (se 2 (se 4 (se 6 ())
 (2 4 6)
Slide 4
Find all the even numbers
(define (find-evens sent)
(cond ((empty? sent)
;base case
'()
)
((odd? (first sent)) ;rec case 1
(find-evens (bf sent)) )
(else
;rec case 2: even
(se (first sent)
(find-evens (bf sent))) )
))
Slide 5
(define (pascal C R)
(cond
((= C 0) 1)
;base case
((= C R) 1)
;base case
(else
;tree recurse
(+ (pascal
C
(- R 1))
(pascal (- C 1) (- R 1))
)))
Slide 6
> (pascal 2 5)
(pascal 2 5)
(+
(pascal 2 4)
(+
(pascal 2 3)
(+
(pascal 2 2)
 1
(pascal 1 2)
(+
(pascal 1 1)
 1
(pascal 0 1)
 1
(pascal 1 2)
(+
(pascal 1 1)
 1
(pascal 0 1)
 1
(pascal 0 2)
 1
(pascal 1 3)
(pascal 1 4)
(+
(pascal 1 3)
(pascal 1 2)
(+
(pascal 0 2)
 1
(pascal 1 1)
 1
(pascal 0 1)
 1
(pascal 0 3)
 1
Slide 7
What is a procedure?
(or, a function).
Slide 8
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))
Slide 9
Consider two forms of “month-name”:
(define (month-name1 date)
(first date))
(define month-name2 first)
Slide 10
Why have procedures as objects?
Other programming languages
don’t (often)
Slide 11
Procedures can be taken
as arguments…
(define (apply-to-5-and-3 func)
(func 5 3))
(define (math-function? func)
(or (equal? func +)
(equal? func -)
(equal? func *)
(equal? func /)))
Slide 12
…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))
Slide 13
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
Slide 14
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
Slide 15
(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))
Slide 16
Download