This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: L AMBDA , H IGHER O RDER F UNCTIONS 3 GEORGE WANG email@example.com Department of Electrical Engineering and Computer Sciences University of California, Berkeley June 23, 2010 1 Review from Last Lecture 1.1 CS Illustrated Functions as Arguments: http://csillustrated.berkeley.edu/PDFs/functions-as-data.pdf . 1.2 Procedures We Learned sentence ;Joins together multiple words in a single sentence se ; same as sentence empty? ; tests if a sentence or a word is empty 1.3 Review of Recursion If we examine both of the procedures from yesterday, we see that they both kind of have that same mapping pattern. You take the first element, you do something to it, and then you combine it with the recursive call on the sentence except for the first element. This pattern is very common. In fact, youll give it a name in the homework. Unfortunately, not everything follows this pattern. Lets look at the code for Pascals Triangle. (define (pascal row col) (cond ((= col 0) 1) ((= col row) 1) (else (+ (pascal (- row 1) (- col 1)) (pascal (- row 1) col) )))) Note that both row and column numbers start at zero. 1.4 Another Higher-Order-Function Example: Keep ;;;;; cs60a/lectures/1.3/keep.scm (define (evens nums) (cond ((empty? nums) ()) ((= (remainder (first nums) 2) 0) 1 (se (first nums) (evens (bf nums))) ) (else (evens (bf nums))) )) (define (ewords sent) (cond ((empty? sent) ()) ((member? e (first sent)) (se (first sent) (ewords (bf sent))) ) (else (ewords (bf sent))) )) (define (pronouns sent) (cond ((empty? sent) ()) ((member? (first sent) (I me you he she it him her we us they them)) (se (first sent) (pronouns (bf sent))) ) (else (pronouns (bf sent))) )) We can again see there is a generalization here: (define (keep pred sent) (cond((empty? sent) ()) ((pred (first sent)) (se (first sent)(keep pred (bf sent)))) (else (keep pred (bf sent))))) 2 Anonymous Functions 2.1 Motivating Factors Lets get back to the generalized sum function we coded yesterday. Just as a reminder, it computes b X i = a f ( i ) ....
View Full Document