1 / 47

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

מבוא מורחב למדעי המחשב בשפת Scheme. תרגול 10. אג'נדה. שאלות מבחינות חזרה על מימוש stream אפשרי. 2. convert. יש לממש את הפונקציה (convert num base) . הפונקציה מחזירה רשימה המייצגת את ספרותיו של num בבסיס base בסדר הפוך (ספרת האחדות ראשונה). (define (convert num base)

kbrunson
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  4. convert-all • - numbersזרם (אולי אינסופי) של מספרים. • bases - רשימה של מספרים. • ממשו את הפונקציה (convert-all numbers bases) שמחזירה רשימה של זרמים כך שהזרם ה-iברשימה מכיל את הייצוג של כל המספרים ב-numbers לפי המספר ה-iב-bases (כל ייצוג כזה הוא רשימה בעצמו). > (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)…]) 4

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

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

  7. common-digit-all (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)) 7

  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? (define in-sorted? (lambda (x stream) (let ((e (__________ stream))) (or (= x e) (______ (____ x e) (__________ x (__________ stream)) ))))) stream-car and > in-sorted? stream-cdr 9

  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 (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 stream-car < cons-stream stream-cdr cons-stream stream-cdr 11

  12. stream-conv • נגדיר קונבולוציה בין רשימת מספרים(a1 a2 .. an) ו-stream אינסופי של מספרים [b1 b2 b3 …]להיות stream אינסופי של מספרים[c1 c2 c3 … ] כך ש- ci= a1*bi+a2*bi+1 ... an*bi+n • ממשו את הפונקצייהstream-conv המחשבת את זרם הקונבולוציה עפ"י ההגדרה הנ"ל. • לדוגמא: (stream-conv ‘(1 2) fibs) צריך להחזיר [2 3 5 8 13 21 34 55 89……] 12

  13. stream-conv (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 13

  14. stream-conv • מהם ערכי x1 ו-x2 בסוף השערוך? / 1 / 2 (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)) 14

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

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

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

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

  19. מודל הסביבות GE L2 L1 p: fb: (define y … p: xb:(+ x 3) (L1 L2) 19

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

  21. מודל הסביבות GE E1 L2 L1 f: y: L3 p: fb: (define y … p: xb:(+ x 3) p: gb:(lambda…) (define y (lambda (g) (lambda (y) (f (g y))))) | E1 21

  22. מודל הסביבות GE E1 L2 L1 f: y: L3 p: fb: (define y … p: xb:(+ x 3) p: gb:(lambda…) ((y f) 3) | E1 22

  23. מודל הסביבות GE E1 L2 L1 f: y: L3 p: fb: (define y … p: xb:(+ x 3) p: gb:(lambda…) ((L3 f) 3) | E1 23

  24. מודל הסביבות GE E1 E2 L2 L1 f: y: g: L3 p: fb: (define y … p: xb:(+ x 3) p: gb:(lambda (y) (f (g y))) ((L3 f) 3) | E1 24

  25. מודל הסביבות GE E1 E2 L2 L1 f: y: g: L3 p: fb: (define y … p: xb:(+ x 3) p: gb:(lambda (y) (f (g y))) (lambda (y) (f (g y)) | E2 25

  26. מודל הסביבות GE E1 E2 L2 L1 f: y: g: L4 L3 p: fb: (define y … p: xb:(+ x 3) p: yb:(f (g y)) p: gb:(…) (L4 3) | E1 26

  27. מודל הסביבות GE E1 E2 E3 L1 L2 f: y: g: y:3 L4 L3 p: fb: … p: xb:(+ x 3) p: yb:(f (g y)) p: gb:(…) (L4 3) | E1 27

  28. מודל הסביבות GE E1 E2 E3 L1 L2 f: y: g: y:3 L4 L3 p: fb: … p: xb:(+ x 3) p: yb:(f (g y)) p: gb:(…) (f (g y)) | E3 => (f 6) | E3 => 9 28

  29. מימוש streams

  30. מימוש stream • מנגנוני force/delay • delay הוא special form (delay <exp>) => (lambda () <exp>) (force <delayed-exp>) => ((<delayed-exp>)) => ((lambda () <exp>)) => <exp> 30

  31. מימוש stream • memoization (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)))) 31

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

More Related