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 the equation has two real roots; a more serious program
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.408069916229755 But it seems a shame to have to dene a named func
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 to vary: ; In file cs61a/lectures/1.3/general.scm (defi
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 about iterative versus recursive processes in 1.3 and in th
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 alphabetically, if we sort the words in our text le, we can jus
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 all, but instead to glue together existing programs that
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 define. Then, we can evaluate expressions using (applic
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 file cs61a/lectures/1.1/argue.scm > (argue (i like spina
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 (concerned with building reliable, ecient mechanisms, b
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 (equal? (last wd) y) (word (bl wd) ies) (word wd s) In fi
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 CS book as the textbook. The only thing wrong with thi
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 passing, local state, and inheritance. You know from secti
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 list of all the instances that have been created in a cert
The structure and interpretation of computer program
COMPUTER S 26275

Spring 2010
; In file cs61a/lectures/3.0/demo1.scm (defineclass (person name) (method (say stuff) stuff) (method (ask stuff) (ask self say (se (would you please) stuff) (method (greet) (ask self say (se (hello my name is) name) ) > (define marc (instantiate person m
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 confused about the terms instance variable versus insta
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 multiple independent intelligent agents; instead of one
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 invoke that shape with the proper message: (circle3 area).
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 programs, so you can write general programs instead of very s
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 types and two operations we need four algorithms. What happ
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 large systems with many small procedures that handle s
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/lectures/2.2/tiny.scm
(define (eval exp) (cond (selfe
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 detailed Scheme interpreter in Chapter 4, which well get
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 tree) (print (entry tree) ) For a tree of arithmetic ope
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, then the grandchildren, and so on. The program is a little
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 to one recursive call, to handle the cdr of the sequen
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 grab the rst pair of this structure and shake it so that
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 has been visited! It means merely that one group of sibli
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 thing we might want to do with a tree is create another
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 branches, numbers at leaves. The name tree comes from the