4. Fortran
LISP (1955, Backus)
Programming Languages (1959, McCarthy)
Algol
Brief History of
(1960)
Scheme
PS1-5 (1975, S&S) C Simula
(1969, Ritchie) (1967)
PS6-7 C++
(1983, Stroustrup)
Python
(1991, von Rossum)
Java
JavaScript PS8
(1995, Sun)
(1995, Eich)
4
5. Why design a new
programming
language?
C: wanted both
portability and
low-level machine
access
5
6. Multics
Ken Thompson and Dennis Ritchie
Windows and Symbian:
not direct descendants of
Unix, but they are mostly
programmed in C
Android iOS
Image: cc http://en.wikipedia.org/wiki/File:Unix_history-simple.svg 6
7. “Here’s how to
succeed: by being
lucky. Grab on to
something that’s
moving pretty fast.
Let yourself be
carried on when
you’re in the right
place at the right
time.”
Dennis Ritchie
7
8. Recap: Names and Places
A name refers to a place for storing a value.
define creates a new place
(set! name expr) changes the value in the place
name to the value of expr
mcons creates a mutable pair of two new places
(set-mcar! pair expr) changes the value in the mcar
place of pair to the value of expr
(set-mcdr! pair expr) changes the value in the mcdr
place of pair to the value of expr
8
9. Application and Places
(lambda (x) …) also creates a new place named x
The passed argument is put in that place
> (define x 3) x:3
> ((lambda (x) x) 4)
x:4
4
>x
3 How are these
places different?
9
10. Location, Location, Location
Places live in frames
An environment is a frame and a pointer to a
parent environment
All environments except the global environment
have exactly one parent environment, global
environment has no parent
Application creates a new environment
10
11. Environments
global + : #<primitive:+>
environment null? : #<primitive:null?>
x:3
The global environment points to the outermost frame.
It starts with all Scheme built-ins defined.
> (define x 3)
11
12. Stateful Definition Evaluation Rule
A definition creates a new place with the
definition’s name in the frame associated with
the evaluation environment. The value in the
place is value of the definition’s expression.
If there is already a place with the name in the
current frame, the definition replaces the old
place with a new place and value.
12
13. Stateful Name Evaluation Rule
To evaluate a name expression, search the
evaluation environment’s frame for a place with a
name that matches the name in the expression.
If such a place exists, the value of the name
expression is the value in that place.
Otherwise, the value of the name expression is the
result of evaluating the name expression in the
parent environment. If the evaluation environment
has no parent, the name is not defined and the
name expression evaluates to an error.
13
14. Evaluating Names
To evaluate a name expression,
search the evaluation global
environment’s frame for a place env x:3
with a name that matches the
name in the expression.
If such a place exists, the value of
the name expression is the value
in that place.
x : 17
Otherwise, the value of the name
expression is the result of
evaluating the name expression
in the parent environment. If the
evaluation environment has no
parent, the name is not defined y:3
and the name expression
evaluates to an error.
How are environments like this created?
14
16. How to Draw a Procedure
A procedure needs both code and an
environment
We’ll see why soon
We draw procedures like this:
Environment
pointer
environment:
parameters: x
body: (+ x x)
16
17. How to Draw a Procedure
(for artists only)
Environment
pointer
x
Input parameters
(+ x x)
(in mouth) Procedure Body
17
18. Procedures
global + : #<primitive:+>
environment null? : #<primitive:null?>
x:3
double:
> (define double environment:
parameters: x
body: (+ x x)
(lambda (x) (+ x x)))
18
19. Application
Old rule: (Substitution model)
Apply Rule 2: Constructed Procedures. To
apply a constructed procedure, evaluate the
body of the procedure with each formal
parameter replaced by the corresponding
actual argument expression value.
19
20. Stateful Application Rule
(Constructed Procedures)
To apply a constructed procedure:
1. Construct a new environment, whose parent is the
environment of the applied procedure.
2. For each procedure parameter, create a place in the
frame of the new environment with the name of the
parameter. Evaluate each operand expression in the
environment or the application and initialize the value
in each place to the value of the corresponding
operand expression.
3. Evaluate the body of the procedure in the newly
created environment. The resulting value is the value
of the application.
20
21. 1. Construct a new global
environment
environment, parent is
procedure’s environment
+ : #<primitive:+>
pointer
2. Make places in that frame
x:3 double:
with the names of each
parameter, and operand
values
environment:
3. Evaluate the body in the parameters: x
new environment body: (+ x x)
x: 4
> (double 4) (+ x x)
8
21
24. Overall Results
5 below 70 5 above 100
Come to my office
hours or arrange a
meeting to go
Solutions/comments will be posted on
over things you course site later by tomorrow.
didn’t understand
24
25. Honor
15. Do you trust your classmates to follow the honor expectations in this class?
— Yes, I trust them completely.
— I worry that there may be a few transgressions, but I believe the vast majority
of the class is honorable and it is fair and beneficial to rely on this.
— I think this class places too high a burden on students’ honor, and there are
enough dishonorable students that it is unfair on the honorable students.
— I have reason to suspect that other students violated the honor policy on
0 problem sets.
— I have direct knowledge of other students violating the honor policy on
0 problem sets.
— I have reason to suspect that other students violated the honor policy on this
0 exam.
0
— I have direct knowledge of other students violating the honor policy on this
exam.
25
27. Problem 11
Define a procedure, count-tandem-repeats, that takes
as input a list, p, and a number, n. The output should be
the total number of times the first n elements of p are
consecutively repeated at the beginning of p.
Repetitions may not overlap.
27