150 likes | 275 Views
מבוא מורחב למדעי המחשב בשפת Scheme. בוחן אמצע אביב 2006 פתרון לדוגמא. Split. > (define syms '(p l a y - i n - e u r o p e - o r - i n - s p a i n)) > (split syms ‘-) ((p l a y) (i n) (e u r o p e) (o r) (i n) (s p a i n)). Split. (define (split symbols sep)
E N D
מבוא מורחב למדעי המחשב בשפת Scheme בוחן אמצע אביב 2006 פתרון לדוגמא
Split > (define syms '(p l a y - i n - e u r o p e - o r - i n - s p a i n)) > (split syms ‘-) ((p l a y) (i n) (e u r o p e) (o r) (i n) (s p a i n))
Split • (define (split symbols sep) • (define (update sym word-lists) • (if (eq? sym sep) • (cons ___________________________________ • ___________________________________ ) • (cons ___________________________________ • ___________________________________))) • (accumulate update (list null) symbols)) null word-lists (cons sym (car word-lists)) (cdr word-lists)
Replace > (define syms '(p l a y - i n - e u r o p e - o r - i n - s p a i n)) > (replace ‘n ‘m syms) (p l a y – i m – e u r o p e – o r – i m – s p a i m) • (define (replace from-sym to-sym symbols) • (map • )) (lambda (s) (if (eq? from-sym s) to-sym s)) symbols)
Accum-replace > (accum-replace ‘((a e) (n m) (p a)) syms) (p l a y – i n – e u r o p e – o r – i n – s p a i n) (a l a y – i n – e u r o a e – o r – i n – s a a i n) (a l a y – i m – e u r o a e – o r – i m – s a a i m) (e l e y – i m – e u r o e e – o r – i m – s ee i m)
Accum-replace • (define (accum-replace from-to-list symbols) • (accumulate • (lambda(p syms) • ( ________________________________ )) • ____________________ • from-to-list)) • )) replace (car p) (cadr p) syms symbols
Extend-replace > (extend-replace ‘((a e) (n m) (p a)) syms) (p l a y – i n – e u r o p e – o r – i n – s pa i n) (a l a y – i n – e u r o a e – o r – i n – s aa i n) (a l a y – i m – e u r o a e – o r – i m – s a a i m) (a l e y – i m – e u r o a e – o r – i m – s a e i m)
Extend-replace • (define (extend-replace from-to-list symbols) • (define (scan sym) • (let ((from-to (filter • _____________________________________ • _____________________________________ ))) • (if (null? from-to) • ___________________________ • ___________________________))) • (map scan symbols)) (lambda (p) (eq? (car p) sym)) from-to-list sym (cadr (car from-to))
Make-perm > (define p1 (make-perm (list 4 2 1 3)))> (p1 1)4> (p1 2)2> (p1 3)1> (p1 4)3 (define (make-perm lst) ________________________________________________ ________________________________________________ ) (lambda (i) (list-ref lst (- i 1)))
Equal-perms? > (define p1 (make-perm (list 4 2 1 3)))> (define p2 (make-perm (list 2 4 1 3)))> (define p3 (make-perm (list 4 2 1 3)))> (equal-perms? p1 p2 4) #f> (equal-perms? p1 p3 4)#t
Equal-perms? (define (equal-perms? perm1 perm2 k) (accumulate (lambda (n y) __________________________________ ) ____________________________ (integers-between 1 k) )) (and (= (perm1 n) (perm2 n)) y) #t
Inverse-perm > (define p1 (make-perm (list 4 2 1 3)))> (define p1-inv (inverse-perm p1))> (p1 1)4> (p1-inv 4)1> (p1-inv (p1 2))2
Inverse-perm (define (inverse-perm perm) (lambda (n) (define (helper i) (if _____________________________ _____________________________ (helper _____________________ ))) (helper 1))) (= (perm i) n) i (+ i 1)
Insert-at-all-positions > (insert-at-all-positions 'new (list 1 2 3))((new 1 2 3) (1 new 2 3) (1 2 new 3) (1 2 3 new)) (define (insert-at-all-positions elem lst) (if (null? lst) (list (list elem)) (cons (cons elem lst) (map (lambda (l) (cons (car lst) l)) (insert-at-all-positions elem (cdr lst))))))
Generate-all-perm-lists (define (generate-all-perm-lists k) (if (= k 1) ______________________________________________ (accumulate _________________________________________ _________________________________________ (map _____________________________________ _____________________________________ _____________________________________ )))) (list (list 1)) append null (lambda (p) (insert-at-all-positions k p)) (generate-all-perm-lists (- k 1))