מבוא מורחב למדעי המחשב בשפת scheme תרגול 5. 2 list utilities scheme...
TRANSCRIPT
מבוא מורחב למדעי המחשב Schemeבשפת
5תרגול
2
List Utilities
• Scheme built-in procedures– (list x y z ...)– (list-ref lst index)– (length lst)– (append lst1 lst2)
• Built-in append can take any number of lists
– (map proc lst)• Built in map can take any number of lists, and a
multi-variable procedure
3
More Utilities
• Non built-in procedures– (enumerate-interval from to)
• Sometimes named integers-between
– (filter pred lst)– (accumulate op init lst)
Return value for
empty list
(lambda (x y) <body>)
x – list element
y – accumulated result for rest of list
<body> - update y with new element x
4
accumulate(define (accumulate proc init lst)(if (null? lst) init
(proc (car lst) (accumulate proc init (cdr lst)))))
(accumulate + 0 (list 1 2 3 4 5))
(accumulate * 1 (list 1 2 3 4))
(accumulate cons null (list 1 2 3))
(accumulate append null
(list (list 1 2) (list 3 4) (list 5)))
15
24
(1 2 3)
(1 2 3 4 5)
5
Eight-Queen Puzzle
6
Solving the puzzle
• Input: board size
• Output: list of solutions
• Need to determine:– Algorithm– Board abstraction– Putting it together
7
Queen Algorithm
• Observation: One queen in each column
• Placing k queens on k columns:– Find all solutions for k-1– For each partial solution, add a column
• Enumerating over all column positions for new queen
– Remove illegal solutions
8
Example
9
Add a queen
(adjoin-position
new-row rest-of-queens)
Add column
with queen
Queen’s position
in columnPartial solution
Implementation depends on board abstraction
10
In every row
(map (lambda (new-row)
(adjoin-position
new-row rest-of-queens))
(enumerate-interval 1 board-size))
Adjoin partial solution
To every possible row
11
To every partial solution
(map
(lambda (rest-of-queens)
(map (lambda (new-row)
(adjoin-position
new-row rest-of-queens))
(enumerate-interval 1 board-size)))
(queen-cols (- k 1))))
Enumerate
Partial solutions
12
Flatten list of lists
(accumulate
append null
(map
(lambda (rest-of-queens)
(map (lambda (new-row)
(adjoin-position
new-row rest-of-queens))
(enumerate-interval 1 board-size)))
(queen-cols (- k 1))))
Each solution in the list is extended to a list of possible
solutions (with an extra column)
13
Deleting illegal solutions
(filter
(lambda (positions) (safe? positions))
(accumulate
append null
(map
(lambda (rest-of-queens)
(map (lambda (new-row)
(adjoin-position
new-row rest-of-queens))
(enumerate-interval 1 board-size)))
(queen-cols (- k 1)))))
Implementation depends on board
abstraction
14
Everything else(define (queens board-size);; Main procedure (define (queen-cols k) ;; Solve for k columns (if (= k 0) ;; Stop condition (list empty-board) ;; Basic solution (depends on abstraction) (filter (lambda (positions) (safe? positions)) (accumulate append null (map (lambda (rest-of-queens) (map (lambda (new-row) (adjoin-position new-row rest-of-queens)) (enumerate-interval 1 board-size))) (queen-cols (- k 1))))))) (queen-cols board-size)) ;; Initial call
15
Board Abstraction
• List of queen positions
• Don’t represent empty columns
• Add next queen to the left
• Tailored to the algorithm’s needs– Can’t represent other board settings
16
Basic features
(define empty-board null)
(define (adjoin-position
new-row rest-of-board)
(cons new-row rest-of-board))
(define (first-position board) (car board))
(define (rest-positions board) (cdr board))
(define (empty? board) (null? board))
17
The safe? predicate
• Check only the leftmost queen– Rest of board was verified before
• Compare to every other queen:– Different row– Different diagonals
18
Possible Implementation
(define (safe? positions)
(define (verify first rest shift)
(or (empty? rest)
(let ((second (first-position rest)))
(and (not (= first second))
(not (= first (+ second shift)))
(not (= first (- second shift)))
(verify first
(rest-positions rest)
(+ shift 1))))))
(verify (first-position positions)
(rest-positions positions) 1))
19
Trie
s a
k
t b
e s
k
t
b
e
trie1 trie2 trie3
trie4
A trie is a tree with a symbol associated with each arc. All symbols associated with arcs exiting the same node must be different.
A trie represents the set of words matching the paths from the root to the leaves (a word is simply a sequence of symbols).
{ sk , t } { be } { ask , at , be } {}
20
Available procedures(empty-trie) - The empty trie
(extend-trie symb trie1 trie2) - A constructor, returns a trie constructed from trie2, with a new arc from its root, associated with the symbol symb, connected to trie1
(isempty-trie? trie) - A predicate for an empty trie
(first-symbol trie) - A selector, returns a symbol on an arc leaving the root
(first-subtrie trie) - A selector, returns the sub-trie hanging on the arc with the symbol returned from (first-symbol trie)
(rest-trie trie) - A selector, returns the trie without the sub-trie (first-subtrie trie) and without its connecting arc
21
word-into-trie
(define (word-into-trie word) (accumulate
word ))
(lambda (c t) (extend-trie c t empty-trie))empty-trie
22
add-word-to-trie(define (add-word-to-trie word trie) (cond ((isempty-trie? trie) ) ((eq? (car word) (first-symbol trie))
)
(else
))
(extend-trie (car word) (add-word-to-trie (cdr word) (first-subtrie trie)) (rest-trie trie))
(extend-trie (first-symbol trie) (first-subtrie trie) (add-word-to-trie word (rest-trie trie))))
(word-into-trie word)
23
trie-to-words(define (trie-to-words trie) (if (isempty-trie? trie) (let ((symb (first-symbol trie)) (trie1 (first-subtrie trie)) (trie2 (rest-trie trie)))
) ) )
(if (isempty-trie? trie1) (append (list (list symb)) (trie-to-words trie2)) (append (map (lambda(w) (cons symb w)) (trie-to-words trie1)) (trie-to-words trie2)))
null
24
sub-trie word trie
(define (sub-trie word trie) (cond ((null? word) ) ((isempty-trie? trie) ) ((eq? (car word) ) ) (else )) )
_ trie ‘NO (first-symbol trie) (sub-trie (cdr word) (first-subtrie trie)) (sub-trie word (rest-trie trie))
25
count-words-starting-with
(define (count-words-starting-with word trie) (let ((sub (sub-trie word trie)))
))
(cond ((eq? sub 'NO) 0) ((isempty-trie? sub) 1) (else (length (trie-to-words sub))))
26
trie implementation(define empty-trie null )
(define (isempty-trie? trie) (null? trie) )
(define (extend-trie symb trie1 trie2) (cons (cons symb trie1) trie2) )
(define (first-symbol trie) (caar trie) )
(define (first-subtrie trie) (cdar trie) )
(define (rest-trie trie) (cdr trie) )