מבוא מורחב למדעי המחשב בשפת scheme תרגול 11. dotted tail notation...
Post on 20-Dec-2015
238 views
TRANSCRIPT
מבוא מורחב למדעי המחשב Scheme בשפת
11תרגול
Dotted tail notation
(define (proc m1 m2 . opt) <body> )
Mandatory Arguments:m1, m2
Mandatory Arguments:m1, m2
List of optional arguments:opt
List of optional arguments:opt
Sum of squares – using lists
(define (sum-of-squares l) (accumulate + 0 (map square l)))
(define (sum-of-squares . l) (accumulate + 0 (map square l)))
Sum of squares – recursive(define (sum-of-squares l) (if (null? l) 0 (+ (square (car l)) (sum-of-squares (cdr l)))))
(define (sum-of-squares . l) (if (null? l) 0 (+ (square (car l)) (sum-of-squares (cdr l)))))
Apply
(apply <proc> <arg list>)
(define (sum-of-squares . l) (if (null? l) 0 (+ (square (car l)) (apply sum-of-squares (cdr l)))))
Invokes a procedure with given arguments:“(proc (car args) (cadr args) ….)”
Invokes a procedure with given arguments:“(proc (car args) (cadr args) ….)”
Environment Model
3.2, pages 238-251
Environments• Binding: a pairing of a name and a value
• Frame: a table of bindings
• Environment: a sequence of frames
• A precise, completely mechanical, description of:• name-rule looking up the value of a variable• define-rule creating a new definition of a var• set!-rule changing the value of a
variable• lambda-rule creating a procedure• application rule applying a procedure
The Environment Model
The Environment Model• Name-rule: A name X evaluated in environment E gives
the value of X in the first frame of E where X is bound• Define-rule: A define special form evaluated in environment E creates
or replaces a binding in the first frame of E• Set!-rule: A set! of variable X evaluated in environment E changes the
binding of X in the first frame of E where X is bound• Lambda-rule: A lambda special form evaluated in environment E
creates a procedure whose environment pointer points to E• Application Rule: To apply a compound procedure P to arguments
1.Create a new frame A2. Make A into an environment E:
A's enclosing environment pointer goes to the same frame as the environment pointer of P
3. In A, bind the parameters of P to the argument values
4. Evaluate the body of P with E as the current environment
• (define (square x) (* x x))
• (square 5)
(define (square x) (* x x))(define (sum-of-squares x y) (+ (square x) (square y)))(define (f a) (sum-of-squares (+ a 1) (* a 2)))
Application: (f 5)
Nested Procedures
(define g (lambda () (lambda (x y) (* x y))))
(define f (g))
(f 3 4) => 12
GE
p:b:(lambda (x y) (* x y))
g:
(define g (lambda () (lambda (x y) (* x y))))
GE
p:b:(lambda (x y) (* x y))
g:f:
p: x yb: (* x y)
E1 empty
(define f (g))
GE
p:b:(lambda (x y) (* x y))
g:f:
p: x yb: (* x y)
E1 empty
(f 3 4)
X=3Y=4
E2
Nested Procedures
(define g
(lambda (z)
(lambda (x y)
(* x y z))))
(define f (g 2))
(f 3 4) => 24
GE
p: zb:(lambda (x y) (* x y z))
g:f:
(define g (lambda (z) (lambda (x y) (* x y z))))
GE
p: zb:(lambda (x y) (* x y z))
g:f:
p: x yb: (* x y z)
E1 Z: 2
(define f (g 2))
GE
p: zb:(lambda (x y) (* x y z))
g:f:
p: x yb: (* x y z)
E1 Z: 2
(f 3 4)
X=3Y=4
E2
Let expressions
(let ((<var> <exp>)) <body>)is syntactic sugar for((lambda (<var>) <body>) <exp>)
(define a 5)(define b 6)(let ((a 2) (c a))
(+ a b c))=((lambda (a c) (+ a b c)) 2 a)
Let – cont.GE
a: 5 b: 6
p: a c
b: (+ a b c)
E1
a: 2
c: 5
(define a 5)(define b 6)(let ((a 2) (c a))
(+ a b c))=((lambda (a c) (+ a b c)) 2
a)
The cash machine(define (make-withdraw balance)
(lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")))
(define W1 (make-withdraw 100))
> W1
>
>(W1 50)
>
>(W1 40)
>
>(W1 20)
>
50
10
Insufficient funds
#<procedure>
(define (make-withdraw balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")))
(define W1 (make-withdraw 100))
(W1 50)
More than one cash machine>(define W1 (make-withdraw 100))
>(define W2 (make-withdraw 100))
>
>(W1 50)
>
>(W2 40)
>
50
60
(define W2 (make-withdraw 100))
30
Mutable list structures
31
(define (set-to-wow! x) (set-car! (car x) 'wow) x)
(define x (list 'a 'b))
(define z1 (cons x x))
(set-to-wow! z1)
((wow b) wow b)
eq? point to the same object, equal? same content
(eq? (car z1) (cdr z1)) is true
(eq? (car z2) (cdr z2)) is false
(define z2
(cons (list 'a 'b)
(list 'a 'b)))
(set-to-wow! z2)
((wow b) a b)
32
Map without list copying(define (map! f s) (if (null? s) 'done (begin (set-car! s (f (car s))) (map! f (cdr s)))))
(define s '(1 2 3 4))(map! square s) => dones => (1 4 9 16)
33
append!
(define (append! x y)(set-cdr! (last-pair x) y)x)
where:(define (last-pair x)(if (null? (cdr x))
x(last-pair (cdr x))))
34
append vs. append!
(define x ‘(a b))
(define y ‘(c d))
(define z (append x y))
z ==> (a b c d)
(cdr x) ==> ?
(define w (append! x y))
w ==> (a b c d)
(cdr x) ==> ?
35
(define (last-pair x)
(if (null? (cdr x))
x
(last-pair (cdr x))))
(define (make-cycle x)
(set-cdr! (last-pair x) x)
x)
(define z (make-cycle (list 'a 'b 'c)))
Cycle
36
What happens?
(last-pair z) => ?
a b c
z
37
Examine the list and determine whether it contains a cycle, whether a program that tried to find the end of the list by taking successive cdrs would go into an infinite loop
(define (is-cycle? c) (define (loop fast slow) (cond ((null? fast) #f) ((null? (cdr fast)) #f) ((eq? (cdr fast) slow) #t) (else (loop (cddr fast)
(cdr slow))))) (loop c c))
is-cycle?
38
Bounded CounterA bounded counter can be incremented or decremented in
steps of 1, until upper and lower bounds are reached.
syntax: (make-bounded-counter init bottom top)
example:(define c (make-bounded-counter 3 1 5))(counter-inc! c) 4(counter-inc! c) 5(counter-inc! c) 5(counter-dec! c) 4
39
List Implementation
Constructor:
(define (make-bounded-counter init bottom top)
(list init bottom top))
Selectors:
(define (counter-value c) (car c))
(define (counter-bottom c) (cadr c))
(define (counter-top c) (caddr c))
40
List Implementation – cont.Mutators:
(define (counter-inc! c) (if (< (counter-value c) (counter-top c)) (set-car! c (+ 1 (counter-value c)))) (counter-value c))
(define (counter-dec! c) (if (> (counter-value c) (counter-bottom c)) (set-car! c (- (counter-value c) 1))) (counter-value c))
question from past exams
41
Make-line
(define (make-line a b) (lambda (x) (cond ((pair? x) (set! a (car x)) (set! b (cdr x)))
(else (+ b (* x a)))) ) ) (define a 4)(define b 5)(define proc (make-line 1 2))
Q1
make-line:a: 4b: 5proc:
GE
p: a bb:(lambda (x)…
a: 1b: 2
E1
p: xb:(cond…
make-line:a: 3b: 5proc:
GE
p: a bb:(lambda (x)…
a: 1b: 2
E1
p: xb:(cond…
x: 1E2
(+ b (* x a))(set! a (proc 1))
make-line:a: 3b: 5proc:
GE
p: a bb:(lambda (x)…
a: 1 3b: 2 4
E1
p: xb:(cond…
x: 3.4E3
(set! a (car x))(set! b (cdr x))(proc (cons 3 4))
make-line:a: 3b: 5proc: c: 7
GE
p: a bb:(lambda (x)…
a: 3b: 4
E1
p: xb:(cond…
x: 1E4
(+ b (* x a))(define c (proc 1))