View
212
Download
0
Category
Preview:
Citation preview
Principles of Programming
Languages Practice session 6
•Sequence Operations•Partial Evaluation•Lazy Lists
The Sequence Interface( , הסימן את לכתוב למשל מערך על כלשהי פעולה לבצע אחד( 1,-0,1רוצים כל של
מהערכים.
:Javaב-◦for(int i=0 ; i < arrayLength ; i++){if( a[i] == 0 )
b[i] = 0 ;else
b[i] = a[i]/abs(a[i]) ;}
:Schemeב-◦
(define signs(lambda (ls)
(if (null? ls) ls(if (= 0 (car ls))
(cons 0 (signs (cdr ls)))(cons (/ (car ls) (abs (car ls))) (signs (cdr ls)))))))
The Sequence Interface( , הסימן את לכתוב למשל אוסף על כלשהי פעולה לבצע אחד( 1,-0,1רוצים כל של
מהערכים.
:Schemeב-◦(define signs(lambda (ls)
(if (null? ls) ls(if (= 0 (car ls))
(cons 0 (signs (cdr ls)))(cons (/ (car ls) (abs (car ls))) (signs (cdr ls)))))))
:mapבאמצעות ◦
(define signs-map(lambda (ls)
(map (lambda (x)(if (= x 0)
0(/ x (abs x))))
ls)))
The Sequence Interfacefilter " . המחזירה : פונקציה י ע נעשה הסינון רשימה מתוך איברים מסננת
. בוליאני ערך>(filter (lambda(x)(even? x)) (list 1 2 3 4 5 6))'(2 4 6)
:הפונקציהשאלת חימוםpartition מקבלת פונקציה בוליאנית ורשימה, ומחלקת את איברי הרשימה לאלו
שעומדים בתנאי של הפונקציה ואלו שלא עומדים בתנאי. ?filter ו-map באמצעותpartitionכיצד נממש את
דוגמת הרצה:> (partition number? (list 1 2 3 'a 'b 'c 1 2 3 4 'd 'e 'f))'((1 2 3 1 2 3 4) a b c d e f)
The Sequence Interface : חימום ורשימה partitionהפונקציהשאלת בוליאנית פונקציה מקבלת
ואלו הפונקציה של בתנאי שעומדים לאלו הרשימה איברי את ומחלקת . את נממש כיצד בתנאי עומדים ?partitionשלא
: ריצה דוגמת> (partition number? (list 1 2 3 'a 'b 'c 1 2 3 4 'd 'e 'f))'((1 2 3 1 2 3 4) a b c d e f)
(define partition(lambda (pred seq)
(cons (filter pred seq)(filter (lambda (x)
(not (pred x))) seq))))
The Sequence Interfaceaccumulate. רשימה : איברי על בינארית פעולה הפעלת
: האחרון האיבר על תופעל הבינארית הפעולה פונקציית הפעולות סדר , , הסוף מן האיברים שאר על מכן ולאחר הניטרלי האיבר ועל ברשימה
. הקודמת, ההפעלה תוצאת עם פעם כל להתחלה> (accumulate + 0 (list 1 2 3 4 5))
15
(+ 5 0) => 5
(+ 4 5) => 9
(+ 3 9) => 12
…
> (accumulate expt 1 (list 4 3 2))
262144
4^(3^(2^1 )) = 262144
The Sequence Interface הפרוצדורה :2שאלה את לממש מעוניינים -flatאנו
sum ( עומק עם רשימות של מקוננת רשימה המקבלת. ) בה האיברים כל סכום את ומחזירה ידוע לא קינון
Signature: flat-sum(ls)Type: [List -> Number]Precondition: ls is recursively made of lists and numbers only.
Examples: (flat-sum '(1 2 3 (1 2 (1 (1 2 3 4) 2 3 (1 2 3 4) 4) 3 4) 4)) => 50
The Sequence Interface הפרוצדורה :2שאלה את לממש מעוניינים -flatאנו
sum ( עומק עם רשימות של מקוננת רשימה המקבלת. ) בה האיברים כל סכום את ומחזירה ידוע לא קינון
ממשק ללא :sequenceמימוש(define flat-sum
(lambda (ls)
(cond ((null? ls) 0)
((number? ls) ls)
((let ((car-sum (flat-sum (car ls)))
(cdr-sum (flat-sum (cdr ls))))
(+ car-sum cdr-sum))))))
The Sequence Interface הפרוצדורה :2שאלה את לממש מעוניינים -flatאנו
sum ( עומק עם רשימות של מקוננת רשימה המקבלת. ) בה האיברים כל סכום את ומחזירה ידוע לא קינון
באמצעות :sequence operationsמימוש(define flat-sum
(lambda (lst)
(acc + 0
(map (lambda (x)
(if (number? x)
x
(flat-sum x)))
lst))))
?
לייעל אפשר איך
The Sequence Interface(define flat-sum
(lambda (lst)
(acc + 0
(map (lambda (x)
(if (number? x)
x
(flat-sum x)))
lst))))
(define flat-sum
(let ((node->number
(lambda (x)
(if (number? x)
x
(flat-sum x)))))
(lambda (ls)
(accumulate + 0 (map node->number ls)))))
: חדשה גרסה
The Sequence Interface לבטל :3שאלה מעוניינים אנו מספרים רשימת בהינתן
. ממש יורד בסדר ולמיינה ברשימה כפילויות , מבוסס יהיה המיון פשטות ?insertion sortלשם נממש . כיצד
:: Iשלב ברשימה מקסימלי איבר מציאתSignature: maximum(lst)
Type: [List(Number) -> Number]
Pre-Condition: lst contains only natural numbers
(define maximum
(lambda (lst)
(acc
(lambda (a b)
(if (> a b)
a
b))
0
lst)))
The Sequence Interface לבטל :3שאלה מעוניינים אנו מספרים רשימת בהינתן
. ממש יורד בסדר ולמיינה ברשימה כפילויות , מבוסס יהיה המיון פשטות ?insertion sortלשם נממש . כיצד
:: IIשלב הרשימה מיוןSignature: insertion-sort(lst)
Type: [List(Number) -> List(Number)]
(define insertion-sort
(lambda (lst)
(if (null? lst)
lst
(let* ((max (maximum lst))
(rest (filter (lambda (x)
(not (= max x))) lst)))
(cons max (insertion-sort rest))))))
Principles of Programming
Languages Practice session 6
•Sequence Operations•Partial Evaluation•Lazy Lists
Partial Evaluation with Currying מימוש :4שאלהaccumulate: בהרצאה שראיתם כפי
(define accumulate
(lambda (op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence))))))
Currying:נאיבי (define c-accumulate-naive
(lambda (op initial)
(lambda (sequence)
(if (null? sequence)
initial
(op (car sequence)
((c-accumulate-naive op initial) (cdr sequence)))))))
Partial Evaluation with CurryingCurrying: נאיבי
(define c-accumulate-naive
(lambda (op initial)
(lambda (sequence)
(if (null? sequence)
initial
(op (car sequence)
((c-accumulate-naive op initial) (cdr sequence)))))))
> (define add-accum (c-accumulate-naive + 0))
> (add-accum '(1 2 3))
6
> (add-accum '(4 5 6))
15
" הרבה " נוצריםCLOSURE -ים
Partial Evaluation with CurryingCurrying: נאיבי
(define c-accumulate-naive
(lambda (op initial)
(lambda (sequence)
(if (null? sequence)
initial
(op (car sequence)
((c-accumulate-naive op initial) (cdr sequence)))))))
:מימוש חכם יותר(define c-accumulate-op-initial
(lambda (op initial)
(letrec ((iter (lambda (sequence)
(if (null? sequence)
initial
(op (car sequence) (iter (cdr sequence)))))))
iter)))
Partial Evaluation with Currying: יותר חכם מימוש
(define c-accumulate-op-initial
(lambda (op initial)
(letrec ((iter (lambda (sequence)
(if (null? sequence)
initial
(op (car sequence) (iter (cdr sequence)))))))
iter)))
> (define add-accum (c-accumulate-naive + 0))
> (add-accum '(1 2 3))
6
> (add-accum '(4 5 6))
15
נוצרים ים- CLOSUREלא
Partial Evaluation with Currying של :4שאלה חלקי נוספת – accumulateשיערוך מבט :נקודתCurrying: נאיבי
(define c-accumulate-naive
(lambda (sequence)
(lambda (op initial)
(if (null? sequence)
initial
(op (car sequence)
((c-accumulate-naive (cdr sequence)) op initial))))))
:לא נאיבי(define c-accumulate-sequence
(lambda (sequence)
(if (null? sequence)
(lambda (op initial) initial)
(let ((rest (c-accumulate-sequence (cdr sequence))))
(lambda (op initial)
(op (car sequence) (rest op initial)))))))
The Sequence Interfaceהפרוצדורה :1שאלה את לממש רוצים
dists_k המייצגים מספרים זוגות של רשימה שמקבלתמהראשית(, x,yקואורדינטות ) המרחק את ומחזירה
המקיימות הנקודות כל .x=kשלSignature: dists_k (ls k)Type: [LIST(Number*Number) ->LIST(Number)]Examples: (dists_k (list (cons 3 4) (cons 3 7) (cons 15 12) (cons 3 12))
3) => '(5 7.615773105863909 12.36931687685298)
: " לנו נתונה ל הנ העזר פונקציית) <define dist (lambda (x y) (sqrt (+ (sqr x) (sqr y))))(
The Sequence Interfaceהפרוצדורה :1שאלה את לממש רשימה dists_k רוצים שמקבלת
קואורדינטות ) המייצגים מספרים זוגות את(, x,yשל ומחזירההמקיימות הנקודות כל של מהראשית .x=kהמרחק
) <define dist (lambda (x y) (sqrt (+ (sqr x) (sqr y))))(
:Sequenceמימוש רקורסיבי, ללא ממשק > (define dists_k (lambda (ls k) (if (null? ls) ls (let ((xcord (caar ls)) (ycord (cdar ls)) (rest (dists_k (cdr ls) k))) (if (= xcord k) (cons (dist xcord ycord) rest) rest)))))
The Sequence Interfaceהפרוצדורה :1שאלה את לממש שמקבלת dists_k רוצים
קואורדינטות ) המייצגים מספרים זוגות של (, x,yרשימההמקיימות הנקודות כל של מהראשית המרחק את ומחזירה
x=k. ) <define dist (lambda (x y) (sqrt (+ (sqr x) (sqr y))))(
:Sequenceמימוש בעזרת ממשק > (define dists_k (lambda (ls k) (map (lambda (x) (dist (car x) (cdr x))) (filter (lambda (x) (= (car x) k)) ls))))
Currying
הפרוצדורה :1שאלה את לממש רוציםdists_k מספרים זוגות של רשימה שמקבלת
קואורדינטות ) את(, x,yהמייצגים ומחזירההמקיימות הנקודות כל של מהראשית המרחק
x=k. >(define dist (lambda (x y) (sqrt (+ (sqr x)
(sqr y)))))
:Sequenceמימוש בעזרת ממשק > (define dists_k (lambda (ls k)
(map (lambda (x)
(dist (car x) (cdr x)))
(filter (lambda (x)
(= (car x) k))
ls))))
?:2שאלה " ל הנ במימוש לשפר אפשר מה
Currying:2שאלה
>(define dist (lambda (x y) (sqrt (+ (sqr x) (sqr y)))))
> (define c_dist (lambda (x) (let ((xx (sqr x))) (lambda(y) (sqrt (+ (sqr y) xx))))))
החדש:dists_kמימוש > (define dists_k (lambda(ls k) (let ((dist_k (c_dist k))) (map (lambda (x) (dist_k (cdr x))) (filter (lambda (x) (= (car x) k)) ls)))))
Principles of Programming
Languages Practice session 6
•Sequence Operations•Partial Evaluation•Lazy Lists
Lazy Lists :סדרתיים נתונים מבני הן עצלות רשימות תזכורת
. מתוכם איברים של ושמירה חישוב של דחייה המאפשריםיתרונות:
◦ . הרשימה איברי כל את בזיכרון לאחסן צורך אין. - סופיות אין סדרות לייצג אף ניתן
שלא – ◦ ייתכן להם נזדקק בו לזמן ברשימה איברים חישוב דחיית. הרשימה איברי לכל נזדקק
של -LazyList: LazyList = ‘() U T*[Emptyהטיפוס>LazyList]
Lazy Lists:ממשק
Signature: cons(head, tail)Type: [T * [Empty->LazyList] -> LazyList]Purpose: Value constructor for lazy lists Signature: head(lzl)Type: [LazyList -> T]Purpose: Selector for the first item of lzl (a lazy list)Pre-condition: lzl is not empty Signature: tail(lzl)Type: [LazyList -> LazyList]Purpose: Selector for the tail (all but the first item) of lzl (a lazy list)Pre-condition: lzl is not empty Signature: nth(lzl, n)Type: [LazyList * Number -> T]Purpose: Gets the n-th item of lzl (a lazy list)Pre-condition: lzl length is at least n, n is a natural numberPost-condition: result=lzl[n-1]
Signature: take(lzl, n)Type: [LazyList * Number -> List]Purpose: Creates a List out of the first n items of lzl (a lazy list)Pre-condition: lzl length is at least n, n is a natural numberPost-condition: result[i]=lzl[i] for any i in range 0::n-1
Lazy Lists חוקי :6שאלה לפי שתספור רשימה לממש .7BOOMרוצים
(define seven-boom!
(lambda (n)
(cons
(cond ((= (modulo n 7) 0) 'boom)
((has-digit? n 7) 'boom)
(else n))
(lambda ()
(seven-boom! (+ n 1))))))
> (seven-boom! 1)
'(1 . #<procedure>)
> (take (seven-boom! 1) 7)
'(1 2 3 4 5 6 boom)
Lazy Lists 7שאלה::' קולאץ הפונקציה השערת הרכבת טבעי מספר לכל
- ל מגיעה עצמה .1על
' עבור קולאץ סדרת את המכילה עצלה רשימה להגדיר nרוציםכלשהו.
(define lzl-collatz
(lambda (n)
(if (< n 2)
(cons n
(lambda () (list)))
(cons n
(lambda ()
(if (= (modulo n 2) 0)
(lzl-collatz (/ n 2))
(lzl-collatz (+ (* 3 n) 1 ))))))))
Lazy Lists את :8שאלה לממש עצלות filterנרצה רשימות עבור
Signature: lzl-filter(pred lzl)
Type: [[T -> Bool] * LazyList-> LazyList]
Purpose: Creates a lazy list containing all members of lzl that fit criteria pred
(define lzl-filter
(lambda (pred lzl)
(cond ((null? lzl) lzl)
((pred (head lzl)) (cons (head lzl)
(lambda ()
(lzl-filter pred (tail lzl)))))
(else (lzl-filter pred (tail lzl))))))
נתון כלשהו:kרשימה עצלה של כל המספרים המתחלקים ב-> (lzl-filter
(lambda (n)
(= (modulo n k) 0))
(integers-from 0)
Recommended