Presentation is loading. Please wait.

Presentation is loading. Please wait.

מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.

Similar presentations


Presentation on theme: "מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2."— Presentation transcript:

1 מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11

2 Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2 List of optional arguments: opt List of optional arguments: opt

3 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)))

4 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)))))

5 Apply (apply ) (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) ….)”

6 Environment Model 3.2, pages 238-251

7 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-rulelooking up the value of a variable define-rulecreating a new definition of a var set!-rulechanging the value of a variable lambda-rulecreating a procedure application rule applying a procedure The Environment Model

8 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 A 2. 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

9 (define (square x) (* x x))

10 (square 5)

11 (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)))

12 Application: (f 5)

13 Nested Procedures (define g (lambda () (lambda (x y) (* x y)))) (define f (g)) (f 3 4) => 12

14 GE p: b:(lambda (x y) (* x y)) g: (define g (lambda () (lambda (x y) (* x y))))

15 GE p: b:(lambda (x y) (* x y)) g: f: p: x y b: (* x y) E1 empty (define f (g))

16 GE p: b:(lambda (x y) (* x y)) g: f: p: x y b: (* x y) E1 empty (f 3 4) X=3 Y=4 E2

17 Nested Procedures (define g (lambda (z) (lambda (x y) (* x y z)))) (define f (g 2)) (f 3 4) => 24

18 GE p: z b:(lambda (x y) (* x y z)) g: f: (define g (lambda (z) (lambda (x y) (* x y z))))

19 GE p: z b:(lambda (x y) (* x y z)) g: f: p: x y b: (* x y z) E1 Z: 2 (define f (g 2))

20 GE p: z b:(lambda (x y) (* x y z)) g: f: p: x y b: (* x y z) E1 Z: 2 (f 3 4) X=3 Y=4 E2

21 Let expressions (let (( )) ) is syntactic sugar for ((lambda ( ) ) ) (define a 5) (define b 6) (let ((a 2) (c a)) (+ a b c)) = ((lambda (a c) (+ a b c)) 2 a)

22 Let – cont. GE a: 5b: 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)

23 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 #

24 (define (make-withdraw balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")))

25 (define W1 (make-withdraw 100))

26 (W1 50)

27

28 More than one cash machine >(define W1 (make-withdraw 100)) >(define W2 (make-withdraw 100)) > >(W1 50) > >(W2 40) > 50 60

29 (define W2 (make-withdraw 100))

30 30 Mutable list structures

31 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 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) => done s => (1 4 9 16)

33 33 append! (define (append! x y) (set-cdr! (last-pair x) y) x)x) where: (define (last-pair x) (if (null? (cdr x)) x (last-pair (cdr x))))

34 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 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 36 What happens? (last-pair z) => ? a b c z

37 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 cdr s 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 38 Bounded Counter A 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-dec! c)  4

39 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 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))

41 question from past exams 41

42 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

43 make-line: a: 4 b: 5 proc: GE p: a b b:(lambda (x)… a: 1 b: 2 E1 p: x b:(cond…

44 make-line: a: 3 b: 5 proc: GE p: a b b:(lambda (x)… a: 1 b: 2 E1 p: x b:(cond… x: 1 E2 (+ b (* x a)) (set! a (proc 1))

45 make-line: a: 3 b: 5 proc: GE p: a b b:(lambda (x)… a: 1 3 b: 2 4 E1 p: x b:(cond… x: 3.4 E3 (set! a (car x)) (set! b (cdr x)) (proc (cons 3 4))

46 make-line: a: 3 b: 5 proc:c: 7 GE p: a b b:(lambda (x)… a: 3 b: 4 E1 p: x b:(cond… x: 1 E4 (+ b (* x a)) (define c (proc 1))


Download ppt "מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2."

Similar presentations


Ads by Google