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

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Mathematical Preliminaries
Finding The Unknown Number In A Number Sentence! NCSCOS 3 rd grade 5.04 By: Stephanie Irizarry Click arrow to go to next question.
Advanced Piloting Cruise Plot.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
and 6.855J Spanning Tree Algorithms. 2 The Greedy Algorithm in Action
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
My Alphabet Book abcdefghijklm nopqrstuvwxyz.
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Solve Multi-step Equations
CS16: Introduction to Data Structures & Algorithms
Turing Machines.
Plt /7/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.2 An Abstraction for Inductive Data Types.
Data Structures Using C++
ABC Technology Project
ML Lists.1 Standard ML Lists. ML Lists.2 Lists A list is a finite sequence of elements. [3,5,9] ["a", "list" ] [] Elements may appear more than once [3,4]
1 Lecture 16: Tables and OOP. 2 Tables -- get and put.
VOORBLAD.
Quadratic Inequalities
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
Squares and Square Root WALK. Solve each problem REVIEW:
© 2012 National Heart Foundation of Australia. Slide 2.
Lets play bingo!!. Calculate: MEAN Calculate: MEDIAN
Chapter 5 Test Review Sections 5-1 through 5-4.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
Addition 1’s to 20.
25 seconds left…...
Week 1.
We will resume in: 25 Minutes.
Depth-First and Breadth-First Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.2 TexPoint fonts used in EMF. Read the TexPoint manual before.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Foundations of Data Structures Practical Session #7 AVL Trees 2.
Towers of Hanoi
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
The 8-queens problem CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Foldr and Foldl CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.5 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
6.001 SICP SICP – October Trees Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7 1. Outline More list examples Symbols 2.
6.001 SICP SICP – October HOPs, Lists, Trees Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
( (lambda (z) (define x (lambda (x) (lambda (y z) (y x)))) ( ( (x (lambda () z)) (lambda (z) z) 3 ) ) ) 2)
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
1 Append: process  (append list1 list2) (cons 1 (append ‘(2) list2)) (cons 1 (cons 2 (append ‘() list2))) (cons 1 (cons 2 list2)) (define (append list1.
Practice session #6: 1. Sequence operations 2. Partial evaluation with currying 3. Lazy-lists.
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Outline 1.The special form quote 2.Data abstraction: Trie 3.Alternative list: Triplets 4.Accumulate-n.
1 מבוא מורחב למדעי המחשב בשפת Scheme תרגול Outline Mutable list structure RPN calculator Vectors and sorting.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 6. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
Racket CSC270 Pepper major portions credited to
6.001 SICP Data abstractions
Lecture 18 Infinite Streams and
PPL Sequence Interface.
Lecture #8 מבוא מורחב.
6.001 SICP Data abstractions
Lecture #7 מבוא מורחב.
List and list operations (continue).
Lecture # , , , , מבוא מורחב.
Presentation 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) ) x – list element y – accumulated result for rest of list - 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 )) (accumulate * 1 (list )) (accumulate cons null (list 1 2 3)) (accumulate append null (list (list 1 2) (list 3 4) (list 5))) (1 2 3) ( )

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 Queens position in column Partial 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 Dont represent empty columns Add next queen to the left Tailored to the algorithms needs –Cant 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 tb 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) )