section05 - Section Notes 5 CS51-Spring 2009 Week of March...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Section Notes 5 CS51—Spring 2009 Week of March 2, 2009 Outline 1. Higher order functions review 2. Complexity and Big-O 3. Induction 4. Exam sample question 5. Design note 1 Goals for today: At the end of today’s section, you should be able to do the following: 1. Brag to your friends that your mind is of a higher order. .. and mean it. 2. Explain to a non-technical person how we analyze the complexity of an operation. 3. Write a recursive function (you already know how to do this). 4. Prove that your recursive function is correct. 5. Extract a recurrence relation for your function. 6. Determine the runtime of your function based on the above. This week’s section will revisit higher order functions and then will focus on what we can learn by analyzing our programs. The former is a tool for writing programs, the latter is a tool for determining that they are correct and efficent. 2 Higher order functions A higher-order function is a function that takes functions as arguments. We will focus on foldr , filter , and map , which implement common patterns in functional programming. 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2.1 Fold Reviewing foldr : We usually think of foldr as combining the elements of a list to produce a non-list, for example computing the sum of a list of numbers. However, given the right function, foldr can also build lists. This effect is achieved by providing a combining operation and a base value that will be used to process the input list. When we call foldr we provide three arguments: a binary function we will call new cons , a value we will call base case , and our input list lst (i.e. (fold new cons base case lst) ). The best way of thinking about foldr is to envision deconstructing a list and replacing the cons holding the list together with the intended function. That is, the following list (define lst (list 1 2 3 4)) is actually: (cons 1 (cons 2 (cons 3 (cons 4 empty())))) . The base case of fold applies where the empty list would be located. For example, summing lst would yield 10, which we obtained by constructing the following replacement (+ 1 (+ 2 (+ 3 (+ 4 0)))) . We can see that 0 is the base case. Therefore, the function sum would be written as follows: (define (sum lst) (foldr + 0 lst)) . The function that replaces the cons can actually still be cons, which would construct a new list. In addition, the function can be written as a lambda that takes 2 arguments - the first of the list that’s returned and the rest of the list that’s returned. Of course, neither of these arguments have to be explicity used in the lambda function; they just have to be written as part of the function because foldr expects the lambda function to take 2 arguments. Why can’t foldr work otherwise? Here we will use it to write
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/26/2009 for the course COMPUTERSC CS51 taught by Professor Gregmorrisett during the Spring '09 term at Harvard.

Page1 / 11

section05 - Section Notes 5 CS51-Spring 2009 Week of March...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online