The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
Let. We write a function that returns a sentence containing the two roots of the quadratic equation ax2 + bx + c = 0 using the formula b b2 4ac x= 2a (We assume, to simplify this presentation, that t
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
Unnamed functions. Suppose we want to compute sin2 5 + sin2 6 + sin2 7 + sin2 8 We can use the generalized sum function this way: > (define (sinsq x) (* (sin x) (sin x) > (sum sinsq 5 8) 2.4080699162
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
In the example about areas, we are generalizing a pattern by using a variable number instead of a constant number. But we can also generalize a pattern in which its a function that we want to be able
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
CS 61A
Lecture Notes
Week 2
Topic: Higherorder procedures Reading: Abelson & Sussman, Section 1.3 Note that we are skipping 1.2; well get to it later. Because of this, never mind for now the stu abou
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
Our plan is to compare each word of the text against the words in the dictionary. But we dont have to read every word of the dictionary for each word of the le; since the dictionary is sorted alphabet
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
Unix Shell Programming [This topic may be in week 1, week 4, or week 11 depending on the holidays each semester.] Sometimes the best way to solve a programming problem is not to write a program at al
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
Normal vs. applicative order. To illustrate this point we use a modied Scheme evaluator that lets us show the process of applicative or normal order evaluation. We dene functions using def instead of
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
Instead of a sequence of events, we have composition of functions, like f (g (x) in high school algebra. We can represent this visually with function machines and plumbing diagrams. Recursion: ; In f
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
Course overview: Computer science isnt about computers (thats electrical engineering) and it isnt primarily a science (we invent things more than we discover them). CS is partly a form of engineering
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
Examples: (define (plural wd) (word wd s) This simple plural works for lots of words (book, computer, elephant) but not for words that end in y (y, spy). So we improve it: ; (define (plural wd) (if (e
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
CS 61A
Lecture Notes
Week 1
Topic: Functional programming Reading: Abelson & Sussman, Section 1.1 (pages 131) Welcome to CS 61A, the worlds best computer science course, because we use the worlds best
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
CS 61A
Lecture Notes
Week 8
Topic: Local state variables, environments Reading: Abelson & Sussman, Section 3.1, 3.2; OOP below the line We said the three big ideas in the OOP interface are message pas
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
Within the default method, the name message refers to whatever message was sent. (The name args refers to a list containing any additional arguments that were used.) Lets say we want to maintain a lis
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
> 2 > > 1 > 3
(ask c1 next) (define c2 (instantiate counter) (ask c2 next) (ask c1 next)
Each counter has its own instance variable to remember how many times its been sent the next message. Dont get
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
CS 61A
Lecture Notes
Week 7
Topic: Objectoriented programming Reading: OOP Abovetheline notes in course reader Midterm 2 is this week. OOP is an abstraction. Above the line we have the metaphor of
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
The defines that produce the individual shapes look no dierent from before, but the results are dierent: Each shape is a function, not a list structure. So to get the area of the shape circle3 we invo
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
Dont get the idea that DDP just means a twodimensional table of operator and type names! DDP is a very general, great idea. It means putting the details of a system into data, rather than into progra
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
Orthogonality of types and operators. The next problem to deal with is the proliferation of functions because you want to be able to apply every operation to every type. In our example, with two type
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
CS 61A
Lecture Notes
Week 6
Topic: Generic operators Reading: Abelson & Sussman, Sections 2.4 through 2.5.2 (pages 169200) The overall problem were addressing this week is to control the complexity of
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
Here is a onescreenful version of a Scheme interpreter, using the substitution model, with most of the details left out: ; (define (scheme) (display "> ") (print (eval (read) (scheme) ) In file cs61a
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
The Scheme1 interpreter [This topic may be in week 5 or in week 6 depending on the holiday schedule.] Were going to investigate a Scheme interpreter written in Scheme. SICP has a rather large and de
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
Postorder: Look at the children before the node. ; In file cs61a/lectures/2.2/print.scm (define (postorder tree) (cond (null? tree) () (else (postorder (leftbranch tree) (postorder (rightbranch t
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
Breadthrst traversal: Look at the siblings before the children. What we want to do is take horizontal slices of the tree. First we look at the root node, then we look at the children of the root, the
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
Tree recursion Compare the car/cdr version of deepmap with ordinary map: (define (map fn seq) (if (null? seq) () (cons (fn (car seq) (map fn (cdr seq) Each nonbasecase invocation of map gives rise
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
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:
But imagine that we g
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
Mutual recursion is what makes it possible to explore the twodimensional tree data structure fully. In particular, note that reaching the base case in forestmap does not mean that the entire tree ha
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
This denition of leaf? should work no matter how we represent the ADT. The straightforward implementation is ; (define maketree cons) (define datum car) (define children cdr) Mapping over trees One t
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
Trees.
Big idea: representing a hierarchy of information.
What are trees good for? Hierarchy: world, countries, states, cities. Ordering: binary search trees. Composition: arithmetic operations at br