Section03 - Section Notes 3 CS51—Spring 2009 Week of Outline 1 Cons Cells 2 Filter 3 Graphs 4 More Practice with Recursion 5 Style 6 Note in the

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Section Notes 3 CS51—Spring, 2009 Week of February 15, 2009 Outline 1. Cons Cells 2. Filter 3. Graphs 4. More Practice with Recursion 5. Style 6. Note: in the interest of saving paper, please return these notes to your section leader at the end of section if you do not intend to keep them. 1 Review By now you should be familiar with the following concepts: • Writing in Scheme • Programming with recursion • Abstract Data Structures • Concrete Data Structures, especially lists and trees 2 Cons cells It has been said that the universe is made of cons cells. Believe it or not, lists in Scheme are made of the same substance. Coincidence? You be the judge. A cons cell is an object that references two other objects. The two references in a cons cell are called the car and the cdr . cons is a useful function that creates a cons cell with the given car (first) and cdr (second): > (cons 1 empty) (list 1) > (cons "left" (cons "right" empty)) (list "left" "right") 1 2.1 Review of proper lists Quick review: there is a specific definition for what constitutes an actual list in Scheme, and here it is: A list is either the empty list, or a pair whose cdr is a list. Notice again that this definition is recursive. Also, in order to define lists, we have to introduce the empty list. The empty list is written as empty and is defined to be the list with no elements. What is the reason for such fussiness in defining lists? (Answer: it provides a base case to test in functions that operate on lists.) Note that the empty list is emphatically not a cons cell. (define (my-list? lst) When dealing with cons cell structures, it is helpful to draw them out with boxes and arrows. Try drawing the structure of the nested list (list (list "a") (list (list "b") (list "c")) (list (list (list "d")))) (which we will call lst ): Now work out the values of these expressions (note that if you find yourself using functions like caaddr too often, you probably need more abstraction). (car lst), (cdr lst), (cadr lst), (cadadr lst), (caaddr lst) Note that the contract for cons is: (-> any/c list? list?) That is, cons takes anything and a list, and produces a list. What would happen if we changed the contract to: (-> any/c any/c list?) 2 3 Filter A filter is a device that lets some inputs through while blocking others. We can imagine applying this idea to Scheme lists. Let’s say we have a list of integers representing the addresses of internet users who have visited our website. We would like to process this list to extract statistics like the number of unique users, the number of users from a particular network (such as Harvard’s campus network), and so on. Filtering functions are quite useful for this task. One thing we must do first is remove addresses that don’t count for various reasons. For example, we know that we obsessively visit our own site every day, so we should remove our address from the list to get more realistic data....
View Full 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 / 8

Section03 - Section Notes 3 CS51—Spring 2009 Week of Outline 1 Cons Cells 2 Filter 3 Graphs 4 More Practice with Recursion 5 Style 6 Note in the

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

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