Presentation is loading. Please wait.

Presentation is loading. Please wait.

מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. אג'נדה שאלות מבחינות חזרה על מימוש stream אפשרי 2.

Similar presentations


Presentation on theme: "מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. אג'נדה שאלות מבחינות חזרה על מימוש stream אפשרי 2."— Presentation transcript:

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

2 אג'נדה שאלות מבחינות חזרה על מימוש stream אפשרי 2

3 convert יש לממש את הפונקציה (convert num base). הפונקציה מחזירה רשימה המייצגת את ספרותיו של num בבסיס base בסדר הפוך (ספרת האחדות ראשונה) 3 (define (convert num base) (if (= num 0) '() ______________________________ ______________________________ )) (cons (remainder num base) (convert (quotient num base) base))

4 convert-all - numbers זרם (אולי אינסופי) של מספרים. bases - רשימה של מספרים. ממשו את הפונקציה (convert-all numbers bases) שמחזירה רשימה של זרמים כך שהזרם ה- i ברשימה מכיל את הייצוג של כל המספרים ב-numbers לפי המספר ה-i ב-bases (כל ייצוג כזה הוא רשימה בעצמו). 4 > (define N (cons-stream 5 (cons-stream 11 (cons-stream 35 …)))) > (define B (list 2 3 4)) > (convert-all N B) ([(1 0 1)(1 1 0 1)(1 1 0 0 0 1)…] [(2 1)(2 0 1)(2 2 0 1)…] [(1 1)(3 2)(3 0 2)…])

5 convert-all 5 (define (convert-all numbers bases) __________________________________ __________________________________ ) (map (lambda (base) (stream-map (lambda (x) (convert x base)) numbers)) bases))

6 common-digits-all ממשו את הפונקציה (common-digits-all numbers bases). הפונקציה מחזירה רשימה של זרמים המכילים #t ו-#f לפי הכלל הבא: במקום ה-k בזרם ה-i יופיע #t אם בייצוג לפי בסיס base i שלnumbers k ו- numbers k+1 יש סיפרה משותפת. אחרת יופיע #f. ניתן להשתמש בפונקצית העזר (common-digit? L1 L2) המחזירה #t אם יש ספרה משותפת ל-L1 ו- L2,ו-#f אחרת. 6

7 common-digit-all 7 (define (common-digit-all nums bases) __________________________________ __________________________________ __________________________________ ) (map (lambda (base) (stream-map (lambda (n1 n2) (common-digit? (convert n1 base) (convert n2 base))) (stream-cdr numbers) numbers)) bases))

8 in-sorted? ממשו את הפונקצייה in-sorted? המקבלת מספר ו-stream אינסופי של מספרים ממוינים בסדר עולה. הפונקציה מחזירה #t אם המספר שהיא מקבלת נמצא בתוך ה-stream ו- #f אחרת. לדוגמא: (in-sorted? 13 fibs) צריך להחזיר #t כי 13 הינו מספר פיבונצ'י ו- (in-sorted? 14 stream-of-fibs) צריך להחזיר #f כי 14 איננו מספר פיבונצ'י. 8

9 in-sorted? 9 (define in-sorted? (lambda (x stream) (let ((e (__________ stream))) (or (= x e) (______ (____ x e) (__________ x (__________ stream)) ))))) stream-car and > in-sorted? stream-cdr

10 merge-inc ממשו את הפונקצייה merge-inc המקבלת שני streams אינסופיים של מספרים ממוינים בסדר עולה. הפונקציה מחזירה stream אינסופי ממוין בסדר עולה המתקבל ממיזוגם של ה- streams הנתונים. אם ישנו מספר המשותף לשני ה-streams אזי הוא יופיע פעמיים ב-stream התוצאה. לדוגמא: (merge-inc fibs (stream-map (lambda (x) (* x 10)) fibs))) צריך להחזיר [0 0 1 1 2 3 5 8 10 10 ] 10

11 merge-inc 11 (define (merge-inc s1 s2) (let ((e1 (__________ s1)) (e2 (__________ s2))) (if (___ e1 e2) (___________ e1 (merge-inc (__________ s1) s2)) (___________ e2 (merge-inc s1 (__________ s2)) ))))) stream-car < cons-stream stream-cdr cons-stream stream-cdr

12 stream-conv נגדיר קונבולוציה בין רשימת מספרים(a 1 a 2.. a n ) ו-stream אינסופי של מספרים [b 1 b 2 b 3 …] להיות stream אינסופי של מספרים[c 1 c 2 c 3 … ] כך ש- c i = a 1 *b i +a 2 *b i+1... a n *b i+n ממשו את הפונקצייהstream-conv המחשבת את זרם הקונבולוציה עפ"י ההגדרה הנ"ל. לדוגמא: (stream-conv ‘(1 2) fibs) צריך להחזיר [2 3 5 8 13 21 34 55 89……] 12

13 stream-conv 13 (define (stream-conv lst str) (define (first-term lst str) (if (null? lst) ___ (___ (* (car lst) (__________ str)) (first-term (__________ lst) (__________ str) )))) (___________ (first-term lst str) (stream-conv lst (__________ str)) )) 0 + stream-car cdr stream-cdr cons-stream stream-cdr

14 stream-conv מהם ערכי x1 ו - x2 בסוף השערוך? 14 (define x1 0) (define x2 0) (define str1 (cons-stream 1 (begin (set! x1 (+ x1 1)) str1))) (define (integers n) (cons-stream n (begin (set! x2 (+ x2 1)) (integers (+ n 1))))) (define str2 (integers 1)) (define c1 (stream-conv '(1 1) str1)) (define c2 (stream-conv '(1 1) str2)) / 1 / 2

15 מודל הסביבות ציירו את מהלך השערוך של הביטוי הבא במודל הסביבות. מהי תוצאת השערוך? 15 (let ((f (lambda (x) (+ x 3)))) (define y (lambda (g) (lambda (y) (f (g y))))) ((y f) 3))

16 מודל הסביבות 16 (let ((f (lambda (x) (+ x 3))))...) => ((lambda (f)...) (lambda (x) (+ x 3) GE

17 מודל הסביבות 17 ((lambda (f)...) (lambda (x) (+ x 3) GE p: f b: (define y … L1

18 מודל הסביבות 18 (L1 (lambda (x) (+ x 3) GE p: f b: (define y … L1 p: x b:(+ x 3) L2

19 מודל הסביבות 19 (L1 L2) GE p: x b:(+ x 3) p: f b: (define y … L2L1

20 מודל הסביבות 20 (L1 L2) GE p: x b:(+ x 3) p: f b: (define y … f: L2L1 E1

21 מודל הסביבות 21 (define y (lambda (g) (lambda (y) (f (g y))))) | E1 GE p: x b:(+ x 3) p: f b: (define y … f: y: p: g b:(lambda…) L2L1 L3 E1

22 מודל הסביבות 22 ((y f) 3) | E1 GE p: x b:(+ x 3) p: f b: (define y … f: y: p: g b:(lambda…) L2L1 L3 E1

23 מודל הסביבות 23 GE p: x b:(+ x 3) p: f b: (define y … f: y: p: g b:(lambda…) L2L1 L3 E1 ((L3 f) 3) | E1

24 מודל הסביבות 24 GE p: x b:(+ x 3) p: f b: (define y … f: y: p: g b:(lambda (y) (f (g y))) g: L2L1 L3 E1E2 ((L3 f) 3) | E1

25 מודל הסביבות 25 GE p: x b:(+ x 3) p: f b: (define y … f: y: p: g b:(lambda (y) (f (g y))) g: L2L1 L3 E1E2 (lambda (y) (f (g y)) | E2

26 מודל הסביבות 26 (L4 3) | E1 GE p: x b:(+ x 3) p: f b: (define y … f: y: p: g b:(…) g: p: y b:(f (g y)) L2L1 L3 L4 E1E2

27 מודל הסביבות 27 (L4 3) | E1 GE p: x b:(+ x 3) p: f b: … f: y: p: g b:(…) g: p: y b:(f (g y)) L1L2 L3 L4 y:3 E1E2E3

28 מודל הסביבות 28 (f (g y)) | E3 => (f 6) | E3 => 9 GE p: x b:(+ x 3) p: f b: … f: y: p: g b:(…) g: p: y b:(f (g y)) L1L2 L3 L4 y:3 E1E2E3

29 מימוש streams

30 מימוש stream מנגנוני force/delay delay הוא special form 30 (delay ) => (lambda () ) (force ) => (( )) => ((lambda () )) =>

31 מימוש stream memoization 31 (define (memo-proc proc) (let ((already-run? #f) (result #f)) (lambda () (if (not already-run?) (begin (set! result (proc)) (set! already-run? #t) result) result))))

32 מימוש stream הגדרה מתוקנת עבור delay: ולכן: 32 (delay ) => (memo-proc (lambda () )) (cons-stream a b) => (cons a (delay b))

33 streams 33 (define ones (cons-stream 1 (begin (set! x (+ x 1)) ones)) GE x:0

34 streams 34 (define ones (cons 1 (delay (begin (set! x (+ x 1)) ones))) GE x:0

35 streams 35 GE x:0 (define ones (cons 1 (memo-proc(lambda ()...)))

36 streams 36 GE x:0 ones: (define ones (cons 1 (memo-proc(lambda ()...))) 1 proc: p:- b:… already-run:#f result: #f p:- b:(begin…)

37 streams 37 GE x:0 ones: (stream-cdr ones) 1 proc: p:- b:… already-run:#f result: #f p:- b:(begin…)

38 streams 38 GE x:0 ones: (force (cdr ones)) 1 proc: p:- b:… already-run:#f result: #f p:- b:(begin…)

39 streams 39 GE x:0 ones: ((cdr ones)) 1 proc: p:- b:… already-run:#f result: #f p:- b:(begin…)

40 streams 40 GE x:0 ones: (not already-run) is #t => (set! result (proc)) 1 proc: p:- b:… already-run:#f result: #f p:- b:(begin…)

41 streams 41 GE x:0 ones: (begin (set! x (+ x 1)) ones) 1 proc: p:- b:… already-run:#f result: #f p:- b:(begin…)

42 streams 42 GE x:01 ones: (begin (set! x (+ x 1)) ones) 1 proc: p:- b:… already-run:#f result: #f p:- b:(begin…) /

43 streams 43 GE x:01 ones: (set! already-run #t) 1 proc: p:- b:… already-run:#f #t result: #f p:- b:(begin…) /

44 streams 44 GE x:01 ones: (stream-cdr ones) 1 proc: p:- b:… already-run:#f #t result: #f p:- b:(begin…) /

45 streams 45 GE x:01 ones: ((cdr ones)) 1 proc: p:- b:… already-run:#f #t result: #f p:- b:(begin…) /

46 streams 46 GE x:01 ones: (not already-run) is #f => result 1 proc: p:- b:… already-run:#f #t result: #f p:- b:(begin…) /

47 streams 47 GE x:01 ones: 1 proc: p:- b:… already-run:#f #t result: #f p:- b:(begin…) /


Download ppt "מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. אג'נדה שאלות מבחינות חזרה על מימוש stream אפשרי 2."

Similar presentations


Ads by Google