Download presentation
Presentation is loading. Please wait.
1
מבוא מורחב למדעי המחשב בשפת Scheme בוחן אמצע אביב 2006 פתרון לדוגמא
2
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))
3
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)
4
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)
5
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 e e i m)
6
Accum-replace (define (accum-replace from-to-list symbols) (accumulate (lambda(p syms) ( ________________________________ )) ____________________ from-to-list)) )) replace (car p) (cadr p) syms symbols
7
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 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) (a l e y – i m – e u r o a e – o r – i m – s a e i m)
8
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))
9
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)))
10
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
11
Equal-perms? (define (equal-perms? perm1 perm2 k) (accumulate (lambda (n y) __________________________________ ) ____________________________ (integers-between 1 k) )) (and (= (perm1 n) (perm2 n)) y) #t
12
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
13
Inverse-perm (define (inverse-perm perm) (lambda (n) (define (helper i) (if _____________________________ _____________________________ (helper _____________________ ))) (helper 1))) (= (perm i) n) i (+ i 1)
14
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))))))
15
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))
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.