170 likes | 360 Views
מבוא מורחב למדעי המחשב בשפת 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
E N D
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 >(memq 1 ((1 2) 2 1 (3 1) 2))=>(1 (3 1) 2) Q1. Write (memq item lst)
memq (define (memq item lst) (cond ((null? lst) #f) ((equal? item (car lst)) lst) (else (memq item (cdr lst)))))
memq (alternative solution) (define (memq item lst) (cond ((null? lst) #f) ((and (atom? (car lst)) (= item (car lst))) lst) (else (memq item (cdr lst)))))
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)
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)) ))))
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)) ))))
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)
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))))))
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
(define (make-star g) (define (g* x) (if (<= x 1) 0 (+ 1 (g* (g x))))) g* ) make-star
(log 4) ==> 2 (log 1) ==> 0 (log 5) ==> 3 (define log (make-star ____________________ ) log (lambda (x) (/ x 2))
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
(define (compose-stars lst) (accumulate _______________________________ _______________________________ (map __________________________ __________________________ ))) compose-stars compose (lambda (x) x) make-star lst
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?
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)) )))