Project: Define the function merge, which consumes two sorted lists of numbers and produces a single sorted list of numbers w/o duplicates. (see lecture 11
(define (f x) (+ (* 3 x x) (* 2 x) 7)) (f 5) = (+ (* 3 5 5) (* 2 5) 7) = (+ 75 10 7) = ...and
(define (f y) (+ (* 3 y y) (* 2 y) 7)) (f 5) = (+ (* 3 5 5) (* 2 5) 7) = (+ 75 10 7) = ...
Why did it not matter that we wrote f with x as the parameter and then f with y?
A placeholder stands for a value. After we replace it with its value, it's useless. The only thing that matters is that we use x when we want to refer to the input position (parameter) x.
Terminology: We say that the all occurrences of x in the body of f are bound to the parameter (aka binding) position of f:
(define (f x) (+ (* 3 x x) (* 2 x) 7))
| ^ ^ ^
| | | |
--------------------
The direction of the arrow suggests that the value flows from the parameter
positions to the bound occurrences.
Terminology: We say that the scope of x is the body of f and that we can rename x to y in its scope. So in
(define (f x) (+ (* 3 x x) (* 2 x) 7))
| ^ ^ ^
| | | |
--------------------
(define (g x) (* x x))
the x of f and the x of g have nothing to do with each other.
Well almost. The scope of f can have a hole:
(define (f x) (+ (* 3 x x) (* 2 x) 7))
(define (g f) (* f f))
| ^ ^
| | |
-------
(define (h y) (f (+ y y)))
A renaming in action of f to fff may only result in:
(define (fff x) (+ (* 3 x x) (* 2 x) 7))
(define (g f) (* f f))
| ^ ^
| | |
-------
(define (h y) (fff (+ y y)))
Draw arrows for fff.
When we discuss new constructs, we must discuss
expression is either
- ...
- ... or:
- (local (... definitions ...) expression)
definition is either
- (define (name name ...) expression)
- (define name expression)
- (define-struct name (name ...))
Example:
(define (f x)
(local ((define (square y) (* y y)))
(* pi (square x))))
Definitions:
(define (f x)
(local ((define (square y) (* y y)))
(* pi (square x))))
-----------------------------------------
Evaluations:
(f 1)
= (local ((define (square y) (* y y)))
(* pi (square 1)))
= (* pi (square 1)) + (define (square y) (* y y)) added to definitions
So now act as if square were a part of the definitions:
Definitions:
(define (f x)
(local ((define (square y) (* y y)))
(* pi (square x))))
(define (square y) (* y y))
-----------------------------------------
Evaluations:
(* pi (square 1))
= (* pi (* 1 1))
= 3.14....
These rules are approximate and partially flawed. They will need an amendment.
(define (sort alon) (cond ...)) (define (insert alon) ...)edit and revise:
(define (sort alon)
(local ((define (sort alon)
(cond ...))
(define (insert alon)
...))
(sort alon)))
Scope! Eval!
| Matthias Felleisen | This page was generated on Fri Apr 9 09:17:38 CDT 1999. |