Info iconThis preview shows pages 1–3. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: G ENERIC O PERATORS 9 GEORGE WANG gswang.cs61a@gmail.com Department of Electrical Engineering and Computer Sciences University of California, Berkeley July 6, 2010 1 Material from Last Week 1.1 About Homework/Projects Many people commented on the length of the projects. However, an equally large number commented on the need for practice problems as well as the help that the homework provided in terms of being able to understand the material. I do admit that I think this homework was fairly sizable in length, but I dont know that it was unreasonable, given that I believe many of the questions should not have taken terribly long. If you find yourself working on a single problem for more than 10 minutes * the number of stars, please consider working in a group or finding some TA help. 1.2 car/cdr recursion Consider the deep list ((a b) (c d)) . Ordinarily we would draw its box and pointer diagram with a horizontal spine at the top and the sublists beneath the spine: b a d c But imagine that we grab the first pair of this structure and shake it so that the pairs fall down as far as they can. Wed end up with this diagram: 1 a b c d Note that these two diagrams represent the same list! They have the same pairs, with the same links from one pair to another. Its just the position of the pairs on the page thats different. But in this new picture, the structure looks a lot like a binary tree, in which the branch nodes are pairs and the leaf nodes are atoms (non-pairs). The left branch of each pair is its car, and the right branch is its cdr. With this metaphor, we can rewrite deep-map to look more like a binary tree program: ;;;;; In file cs61a/lectures/2.2/tree3.scm (define (deep-map fn xmas) (cond ((null? xmas) ()) ((pair? xmas) (cons (deep-map fn (car xmas)) (deep-map fn (cdr xmas)))) (else (fn xmas)))) (The formal parameter xmas reflects the fact that the picture looks kind of like a Christmas tree.) This procedure violates data abstraction! Ordinarily when dealing with lists, we write programs that treat the car and the cdr differently, reflecting the fact that the car of a pair is a list element, whereas the cdr is a sublist. But here we treat the car and the cdr identically. One advantage of this approach is that it works even for improper lists: > (deep-map square ((3 . 4) (5 6)) ((9 . 16) (25 36)) 2 Motivation Last week, we talked about how to abstract a single procedure away from the implementation. Today, well talk abou6t The overall problem were addressing is to control the complexity of large systems with many small proce- dures that handle several types of data. We are building toward the idea of object-oriented programming, which many people see as the ultimate solution to this problem, and which we discuss next week....
View Full Document

This note was uploaded on 08/14/2010 for the course EECS 61A taught by Professor Harvey during the Summer '08 term at University of California, Berkeley.

Page1 / 5


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