1 / 17

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

מבוא מורחב למדעי המחשב בשפת Scheme. תרגול 7. Outline. List exercises. Q1: memq. If item does not appear in lst , returns false. Otherwise – returns the sublist beginning with the item. >( memq 2 (2 1 2 3)) => (1 2 3 ) >(memq 1 ((1 2) 2 3)) => #f

page
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 תרגול 7

  2. Outline List exercises

  3. Q1: memq • If item does not appear in lst, returns false. • Otherwise – returns the sublist beginning with the item. >(memq 2 (2 1 2 3)) => (1 2 3) >(memq 1 ((1 2) 2 3)) => #f >(memq 1 ((1 2) 2 1 (3 1) 2))=>(1 (3 1) 2) Q1. Write (memq item lst)

  4. memq (define (memq item lst) (cond ((null? lst) #f) ((equal? item (car lst)) lst) (else (memq item (cdr lst)))))

  5. memq (alternative solution) (define (memq item lst) (cond ((null? lst) #f) ((and (atom? (car lst)) (= item (car lst))) lst) (else (memq item (cdr lst)))))

  6. Q2: remove-item • Removes the first occurrence of item from lst. >(remove-item 1 (4 1 2 3 1)) => (4 2 3 1) >(remove-item 2 (1 2 3)) => (1 3) >(remove-item 1 (2 3 4)) =>(2 3 4) Q2. Write (remove-item item lst)

  7. remove-item (define (remove-item item lst) (cond ((null? lst) '()) (( equal? item (car lst)) cdr lst)) (else (cons (car lst) (remove-item item (cdr lst)) ))))

  8. remove-item (alternative solution) (define (remove-item item lst) (cond ((null? lst) '()) ((and (atom? (car lst) (= item (car lst)) cdr lst)) (else (cons (car lst) (remove-item item (cdr lst)) ))))

  9. Q3: remove-item* Removes all occurrences of item from lst, at all levels >(remove-item* 1 (1 2)) => (2) >(remove-item* 1 (1 (2 1) 3 (1)) => ((2) 3 ()) Q3. Write (remove-item* item lst)

  10. remove-item* (define (remove-item* item lst) (cond ((null? lst) '()) ((atom? (car lst)) (if (equal? (car lst) item) (remove-item* item (cdr lst)) (cons (car lst) (remove-item* item (cdr lst))))) (else (cons (remove-item* item (car lst)) (remove-item* item (cdr lst))))))

  11. g*(x) = number of times we need to apply g until g(g(g(…(g(x)…)))<=1 Q4.1. Write (g* x) Q4.2. Write (make-star g) That given a function g, returns g* Q4.3: How can we compute log based on make-star? Q4: make-star

  12. (define (make-star g) (define (g* x) (if (<= x 1) 0 (+ 1 (g* (g x))))) g* ) make-star

  13. (log 4) ==> 2 (log 1) ==> 0 (log 5) ==> 3 (define log (make-star ____________________ ) log (lambda (x) (/ x 2))

  14. Input: a list of functions (f g h) Output: a composite function: fgh*(x) := f*(g*(h*(x))) Idea: Make a list of star functions Compose them together Q5: compose-stars

  15. (define (compose-stars lst) (accumulate _______________________________ _______________________________ (map __________________________ __________________________ ))) compose-stars compose (lambda (x) x) make-star lst

  16. Q6: Accumulate-n Almost same as accumulate Takes third argument as “list of lists” Example: > (accumulate-n + 0 ‘((1 2 3) (4 5 6) (7 8 9) (10 11 12))) (22 26 30) Q6.1. Write (accumulate-n op init seqs) Q6.2. What is the time-complexity?

  17. Accumulate-n (define (accumulate-n op init seqs) (if (null? (car seqs)) '() (cons (accumulate op init (map car seqs)) (accumulate-n op init (map cdr seqs)) )))

More Related