List Utilities Scheme builtin procedures list x y
List Utilities • Scheme built-in procedures – (list x y z. . . ) – (list-ref lst index) – (length lst) – (append lst 1 lst 2) • 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 2
More Utilities • Non built-in procedures – (enumerate-interval from to) • Sometimes named integers-between – (filter pred lst) – (accumulate op init lst) (lambda (x y) <body>) x – list element Return value for empty list y – accumulated result for rest of list <body> - update y with new element x 3
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)) 15 (accumulate * 1 (list 1 2 3 4)) 24 (accumulate cons null (list 1 2 3)) (1 2 3) (accumulate append null (1 2 3 4 5) (list 1 2) (list 3 4) (list 5))) 4
Eight-Queen Puzzle 5
Solving the puzzle • Input: board size • Output: list of solutions • Need to determine: – Algorithm – Board abstraction – Putting it together 6
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 7
Example 8
Add a queen Implementation depends on board abstraction Add column with queen (adjoin-position new-row rest-of-queens) Queen’s position in column Partial solution 9
In every row Adjoin partial solution (map (lambda (new-row) (adjoin-position new-row rest-of-queens)) (enumerate-interval 1 board-size)) To every possible row 10
To every partial solution Enumerate (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)))) Partial solutions 11
Flatten list of lists Each solution in the list is extended to a list of possible solutions (with an extra column) (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)))) 12
Deleting illegal solutions (filter (lambda (positions) (safe? positions)) (accumulate append null Implementation (map depends on board (lambda (rest-of-queens) abstraction (map (lambda (new-row) (adjoin-position new-row rest-of-queens)) (enumerate-interval 1 board-size))) (queen-cols (- k 1))))) 13
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 14
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 15
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)) 16
The safe? predicate • Check only the leftmost queen – Rest of board was verified before • Compare to every other queen: – Different row – Different diagonals 17
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-positions) (rest-positions) 1)) 18
Trie 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). s t s e k b a b t e trie 4 k trie 1 trie 2 { sk , t } { be } 19 trie 3 { ask , at , be } {}
Available procedures (empty-trie) - The empty trie (extend-trie symb trie 1 trie 2) - A constructor, returns a trie constructed from trie 2, with a new arc from its root, associated with the symbol symb, connected to trie 1 (isempty-trie? trie) (first-symbol trie) root - A predicate for an empty trie - A selector, returns a symbol on an arc leaving the (first-subtrie) - A selector, returns the sub-trie hanging on the arc with the symbol returned from (first-symbol trie) (rest-trie) - A selector, returns the trie without the sub-trie (firstsubtrie) and without its connecting arc 20
word-into-trie (define (word-into-trie word) (accumulate (lambda (c t) (extend-trie c t empty-trie)) empty-trie word ) ) 21
add-word-to-trie (define (add-word-to-trie word trie) (cond ((isempty-trie? trie) (word-into-trie word) ) ((eq? (car word) (first-symbol trie)) (extend-trie (car word) (add-word-to-trie (cdr word) (first-subtrie)) ) (rest-trie)) (else (extend-trie (first-symbol trie) (first-subtrie) (add-word-to-trie word (rest-trie)))) )) 22
trie-to-words (define (trie-to-words trie) (if (isempty-trie? trie) null (let ((symb (first-symbol trie)) (trie 1 (first-subtrie)) (trie 2 (rest-trie))) (if (isempty-trie? trie 1) (append (list symb)) (trie-to-words trie 2)) (append (map (lambda(w) (cons symb w)) (trie-to-words trie 1)) (trie-to-words trie 2))) ) 23
sub-trie word trie (define (sub-trie word trie) (cond ((null? word) _ trie ) ((isempty-trie? trie) ‘NO ) ((eq? (car word) (first-symbol trie) ) (sub-trie (cdr word) (first-subtrie)) ) (else (sub-trie word (rest-trie)) ) 24
count-words-startingwith (define (count-words-starting-with word trie) (let ((sub-trie word trie))) (cond ((eq? sub 'NO) 0) ((isempty-trie? sub) 1) (else (length (trie-to-words sub)))) ) ) 25
trie implementation (define empty-trie null ) (define (isempty-trie? trie) (null? trie) ) (define (extend-trie symb trie 1 trie 2) (cons symb trie 1) trie 2) ) (define (first-symbol trie) (caar trie) ) (define (first-subtrie) (cdar trie) ) (define (rest-trie) (cdr trie) ) 26
- Slides: 26