מבוא מורחב למדעי המחשב בשפת scheme תרגול 5. 2 list utilities scheme...

26
בבבב בבבבב בבבבב בבבבב בבבבScheme ללללל5

Upload: maximo-creswell

Post on 28-Mar-2015

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

מבוא מורחב למדעי המחשב Schemeבשפת

5תרגול

Page 2: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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

Page 3: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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

Page 4: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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)

Page 5: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

5

Eight-Queen Puzzle

Page 6: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

6

Solving the puzzle

• Input: board size

• Output: list of solutions

• Need to determine:– Algorithm– Board abstraction– Putting it together

Page 7: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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

Page 8: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

8

Example

Page 9: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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

Page 10: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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

Page 11: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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

Page 12: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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)

Page 13: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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

Page 14: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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

Page 15: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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

Page 16: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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))

Page 17: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

17

The safe? predicate

• Check only the leftmost queen– Rest of board was verified before

• Compare to every other queen:– Different row– Different diagonals

Page 18: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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))

Page 19: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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 } {}

Page 20: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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

Page 21: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

21

word-into-trie

(define (word-into-trie word) (accumulate

word ))

(lambda (c t) (extend-trie c t empty-trie))empty-trie

Page 22: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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)

Page 23: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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

Page 24: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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))

Page 25: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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))))

Page 26: מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append

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) )