Lecture 2 Quick recap: Lambda, substitution model, types Recursion Block structure and scope - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Lecture 2 Quick recap: Lambda, substitution model, types Recursion Block structure and scope

Description:

Environment Table. Value. Name. square. Proc (x)(* x x) ???? ????? ... To find an approximation of square root of x, use the following recipe: Make a guess G ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 40
Provided by: johnc92
Category:

less

Transcript and Presenter's Notes

Title: Lecture 2 Quick recap: Lambda, substitution model, types Recursion Block structure and scope


1
Lecture 2 - Quick recap Lambda,
substitution model, types- Recursion- Block
structure and scope
2
Lambda
  • The use of the word lambda is taken from lambda
    calculus.
  • Introduced in the course Discrete Math.
  • Useful notation for functions.

3
Lambda special form
  • lambda
  • (lambda (x y) ( x y x 2))
  • 1st operand position the parameter list (x y)
  • a list of names (perhaps empty)
  • 2nd operand position the body ( x y x 2)
  • may be any sequence of expressions
  • The value of a lambda expression is a compound
    procedure.

4
Evaluating expressions
To Apply a compound procedure (to a list of
arguments) Evaluate the body of the
procedure with the formal parameters replaced by
the corresponding actual values
gt ((lambda(x)( x x)) 5)
5
Naming procedures
  • An application of an unnamed procedure
  • ((lambda (x) ( x x)) 4) gt 16
  • 2. Naming the procedure
  • (define square (lambda (x) ( x x)))
  • (square 3)


6
Naming procedures (cont.)
(define square (lambda (x) ( x x)))
7
Using Abstractions
gt (define square (lambda(x)( x x)))
Environment Table
gt (square 3)
square
Proc (x)( x x)
9
gt ( (square 3) (square 4))
8
Using the substitution model
(define square (lambda (x) ( x x)))(define
average (lambda (x y) (/ ( x y) 2))) (average 5
(square 3))(average 5 ( 3 3))(average 5
9) first evaluate operands, then substitute
(/ ( 5 9) 2)(/ 14 2) if operator is a
primitive procedure, 7 replace by result of
operation

9
Yet More Abstractions
gt (define sum-of-two-squares
(lambda(x y)( (square x) (square y))))
gt (sum-of-two-squares 3 4)
25
gt (define f (lambda(a)
(sum-of-two-squares ( a 3) ( a 3))))
Try it outcompute (f 3) on your own
10
Syntactic Sugar for naming procedures

Instead of writing
(define square (lambda (x) ( x x))
We can write
(define (square x) ( x x))

11
Some examples
(define twice
) (twice 2) gt 4 (twice 3) gt 6
(lambda (x) ( 2 x))
Using syntactic sugar (define (twice x) ( 2
x))
(define second
) (second 2 15 3) gt 15 (second 34 -5 16) gt -5
(lambda (x y z) y)
Using syntactic sugar (define (second x y z) y)
12
Values and types
In scheme almost every expression has a value
Examples
  • The value of 23 is 23
  • The value of is a primitive procedure for
    addition
  • The value of (lambda (x) ( x x)) is the compound
    procedure proc (x) ( x x)

Values have types. For example
  • The type of 23 is numeral
  • The type of is a primitive procedure
  • The type of proc (x) ( x x) is a compound
    procedure
  • The type of (gt x 1) is a boolean (or logical)

13
Booleans
Two distinguished values denoted by the constants
t and f
The type of these values is boolean
gt (lt 2 3)
t
gt (lt 4 3)
f
14
No Value?
  • In scheme almost every expression has a value
  • Why almost?
  • Example what is the value of the expression
  • (define x 8)
  • In scheme, the value of a define expression is
    undefined . This means implementation-dependent
  • Dr. Scheme does not return (print) any value
    for a define expression.
  • Other interpreters may act differently.

15
Recursive Procedures
  • How to create a process of unbounded length?
  • Needed to solve more complicated problems.
  • Start with a simple example.

16
Example Sum of squares
  • S(n) 02 12 22 . (n-1)2 n2

Wishful thinking if I could only solve the
smaller instance
17
An algorithm for computing sum of squares
  • (define sum-squares (lambda (n) (if ( n 0)
    0 ( (sum-squares (- n 1)) (square n))))

18
Evaluating (sum-squares 3)
(define (sum-squares n) (if ( n 0) 0 (
(sum-squares (- n 1)) (square n))))
(sum-squares 3) (if ( 3 0) 0 ( (sum-squares (-
3 1)) (square 3))) ( (sum-squares (- 3 1))
(square 3)) ( (sum-squares (- 3 1)) ( 3 3)) (
(sum-squares (- 3 1)) 9) ( (sum-squares 2) 9) (
(if ( 2 0) 0 ( (sum-squares (- 2 1)) (square
2))) 9) ( ( (sum-squares 1) 4) 9) ( ( (
(sum-squares 0) 1) 4) 9) ( ( ( (if ( 0 0) 0
( (sum-squares (- 0 1)) (square 0))) 1) 4) 9) (
( ( 0 1) 4) 9) 14
What would have happened if if was a function ?
19
Evaluating (sum-squares 3) with IF as regular form
(define (sum-squares n) (if ( n 0) 0 (
(sum-squares (- n 1)) (square n))))
  • (sum-squares 3)
  • (if ( 3 0) 0 ( (sum-squares (- 3 1)) (square
    3)))
  • (if f 0 ( (sum-squares 2) 9))
  • (if f 0 ( (if f 0 ( (sum-squares 1) 4)) 9))
  • (if f 0 ( (if f 0 ( (if f 0 ( (sum-squares
    0) 1)) 4)) 9))
  • (if f 0 ( (if f 0 ( (if f 0 ( (if t 0 (
    (sum-squares -1) 0)) 1)) 4)) 9))
  • ..

We evaluate all operands. We always call
(sum-squares) again. We get an infinite
loop.. OOPS
20
General form of recursive algorithms
  • test, base case, recursive case
  • (define sum-sq (lambda (n)
  • (if ( n 0) test for base case
  • 0 base case
  • ( (sum-sq (- n 1)) (square n))
  • recursive case
  • )))
  • base case small (non-decomposable) problem
  • recursive case larger (decomposable) problem
  • at least one base case, and at least one
    recursive case.

21
Another example of a recursive algorithm
  • even?
  • (define even? (lambda (n)
  • (not (even? (- n 1))) recursive case
  • )))

(if ( n 0) test for base case t
base case
22
Short summary
  • Design a recursive algorithm by
  • 1. Solving big instances using the solution to
    smaller instances.
  • 2. Solving directly the base cases.
  • Recursive algorithms have
  • 1. test
  • 2. recursive case(s)
  • 3. base case(s)

23
Block Structure
  • Lets write a procedure that given x, y, and z
    computes

f(x,y,z) (xy)2 (xz)2
(define (sum-and-square x y) (square ( x y)))
(define (f x y z) ( (sum-and-square x y)
(sum-and-square x z)))
24
Block structure (cont.)
Lets write a procedure that given inputs x, y,
and z, computes
f(x,y,z) (xy)2 (xz)2
while keeping sum-and-square private to f (hidden
from the outside world)
(define (f x y z) (define (sum-and-square x y)
(square ( x y))) ( (sum-and-square x y)
(sum-and-square x z)))
25
We still need to clarify the substitution model..
(define (f x y z) (define (sum-and-square x y)
(square ( x y))) ( (sum-and-square x y)
(sum-and-square x z)))
gt (f 1 2 3)
26
Bounded variables and scope
A procedure definition binds its formal parameters
The scope of the formal parameter is the body of
the procedure. This is called lexical scoping
27
Evaluation of An Expression (refined)
To Apply a compound procedure (to a list of
arguments) Evaluate the body of the
procedure with the formal parameters replaced by
the corresponding actual values. Do not
substitute for occurrences that are bound by an
internal definition.
28
The refined substitution model
(define (f x y z) (define (sum-and-square x y)
(square ( x y))) ( (sum-and-square x y)
(sum-and-square x z)))
gt (f 1 2 3)
29
The refined substitution model
gt (f 1 2 3)
(define (sum-and-square x y) (square ( x
y))) ( (sum-and-square 1 2)
(sum-and-square 1 3)))
30
Computing SQRT A Numeric Algorithm
  • To find an approximation of square root of x,
    use the following recipe
  • Make a guess G
  • Improve the guess by averaging G and x/G
  • Keep improving the guess until it is good enough

31
(define initial-guess 1.0) (define precision
0.0001)
(define (sqrt-iter guess x) (if (good-enough?
guess x) guess (sqrt-iter (improve
guess x) x)))
(define (good-enough? guess x) (lt (abs (-
(square guess) x)) precision))
(define (improve guess x) (average guess (/ x
guess)))
(define (sqrt x) (sqrt-iter initial-guess x))
32
Good programming Style
1. Divide the task to well-defined, natural,
and simple sub-tasks. E.g good-enough?
and improve . Rule of thumb If you can easily
name it, it does a well-defined task.
2. Use parameters. E.g. precision, initial-guess.
3. Use meaningful names.
33
Procedural abstraction
  • It is better to
  • Export only what is needed
  • Hide internal details.

The procedure SQRT is of interest for the
user. The procedure improve-guess is an internal
detail.
  • Exporting only what is needed leads to
  • A clear interface
  • Avoids confusion

34
Rewriting SQRT (Block structure)

(define (sqrt x)
(define (sqrt-iter guess x) (if (good-enough?
guess x) guess (sqrt-iter
(improve guess x) x)))
(define (good-enough? guess x) (lt (abs (-
(square guess) x)) precision))
(define (improve guess x) (average guess (/ x
guess)))
(define initial-guess 1.0) (define precision
0.00001)
(sqrt-iter initial-guess x))
35
Further improving sqrt
Note that in every application of sqrt we
substitute for x the same value in all subsequent
applications of compound procedures ! Therefore
we do not have to explicitly pass x as a formal
variable to all procedures. Instead, can leave it
unbounded (free).
36
SQRT again, taking advantage of the refined
substitution model
  • (define (sqrt x)
  • (define (good-enough? guess)
  • (lt (abs (- (square guess) x)) precision))
  • (define (improve guess)
  • (average guess (/ x guess)))
  • (define (sqrt-iter guess)
  • (if (good-enough? guess)
  • guess
  • (sqrt-iter (improve guess))))
  • (define initial-guess 1.0)
  • (define precision 0.00001)
  • (sqrt-iter initial-guess))

37
SQRT (cont.)
  • gt(sqrt 2)

(define (good-enough? guess) (lt (abs (-
(square guess) 2)) precision)) (define (improve
guess) (average guess (/ 2 guess))) (define
(sqrt-iter guess) (if (good-enough? guess)
guess (sqrt-iter (improve guess))))
(define initial-guess 1.0) (define precision
0.00001) (sqrt-iter initial-guess))
38
Lexical Scoping - again
The lexical scoping rules means that the value of
a variable which is unbounded (free) in a
procedure f is taken from the procedure in which
f was defined. It is also called static scoping
39
Another example for lexical scope
(define (proc1 x) (define (proc2 y) ( x y))
(define (proc3 x) (proc2 x)) (proc3 ( 2 x)))
(proc1 4) proc1.x 4 (proc3 8) proc3.x
8 (proc2 8) proc2.y 8 proc2.xproc1.x4 12
Write a Comment
User Comments (0)
About PowerShow.com