L-System Fractals & Procedure Practice One-Slide
Total Page:16
File Type:pdf, Size:1020Kb
One-Slide Summary • Recursive transition networks and Backus-Naur Form L-System context-free grammars are equivalent formalisms for Fractals specifying formal languages. & • find-closest is quite powerful. Problem sets? • L-system fractals are based on a rewriting system that Procedure is very similar to BNF grammars. Practice • We can practice our CS knowledge up to this point to solve problems by writing recursive procedures. (It won't take too long.) Kinga Dobolyi #1 #2 Outline Recursive Transition Networks • Briefly: Recursive Transition Networks ORNATE NOUN – vs. Backus-Naur Form Grammars • Playing Poker – Revenge of find-closest begin ARTICLE ADJECTIVE NOUN end • PS3 L-System Fractals • Solving Problems ORNATE NOUN ::= OPTARTICLE ADJECTIVES NOUN ADJECTIVES ::= ADJECTIVE ADJECTIVES – Problem Representation ADJECTIVES ::= ε – Important Functions OPTARTICLE ::= ARTICLE OPTARTICLE ::= ε Recall: the two notations are equivalent. #3 #4 Problem Sets PS2: Question 4 • Not just meant to review stuff you should Why is already know (define (higher-card? card1 card2) – Get you to explore new ideas (> (card-rank card1) (card-rank card2) – Motivate what is coming up in the class better than • The main point of the PSs is learning, not (define (higher-card? card1 card2) evaluation (> (car card1) (car card2)) – Don’t give up if you can’t find the answer in the book (you won’t solve many problems this way) ? – Do discuss with other students – (This is why they are difficult.) #5 #6 PS2: Question 9, 10 Can we do better? • Predict how long it will take • Identify ways to make it faster This is what we used in PS2 for our Poker-Bot: (define (find-best-hand hole-cards community-cards) Most of next week and much of many later (car (sort (possible-hands hole-cards classes will be focused on how computer community-cards)) scientists predict how long programs will take, higher-hand?)) and on how to make them faster. But didn't we learn something in the last class for finding the “closest” or “best” element in a list? #7 #8 Recall From Last Time find-bestest (define (find-closest goal lst closeness) (define (find-bestiest lst bestiness) (if (= 1 (length lst)) (if (= 1 (length lst)) (car lst) (car lst) (pick-closest closeness goal (car lst) (pick-bestier bestiness (find-closest goal (cdr lst) closeness)))) (car lst) (find-bestiest (cdr lst) bestiness)))) (define (pick-closest closeness goal num1 num2) (define (pick-bestier bestiness num1 num2) (if (< (closeness goal num1) (if (bestiness num1 num2) (closeness goal num2)) num1 num1 We could use these num2)) num2)) to find the This used to be best hand! (< (dist num1 goal) (dist num2 goal)) #9 #10 Liberal Arts Trivia: find-best-hand Latin American Studies (define (find-bestiest lst bestiness) (if (= 1 (length lst)) (car lst) • This important leader of Spanish America's (pick-bestier bestiness successful struggle for independence is (car lst) credited with decisively contributing to the (find-bestiest (cdr lst) bestiness)))) independence of the present-day countries of (define (pick-bestier bestiness num1 num2) (if (bestiness num1 num2) num1 num2)) Venezuela, Colombia, Ecuador, Peru, Panama, and Bolivia. He defeated the Spanish Monarchy (define (find-best-hand lst) and was in turn defeated by tuberculosis. (find-bestest lst higher-hand?)) Next week: how much better is this? At home: convince yourself that they get the same answer. #11 #12 Liberal Arts Trivia: Media Studies PS3: • This 1988 book by Herman and Chomsky presented the seminal “propaganda model”, arguing that as news media outlets are run Lindenmayer System Fractals by corporations, they are under competitive pressure. Consider the dependency of mass media news outlets upon major sources of news, particularly the government. If a particular outlet is in disfavor with a government, it can be subtly 'shut out', and other outlets given preferential treatment. Since this results in a loss in news leadership, it can also result in a loss of viewership. That can itself result in a loss of advertising revenue, which is the primary income for most of the mass media (newspapers, magazines, television). To minimize the possibilities of lost revenue, therefore, outlets will tend to report news in a tone more favorable to government and business, and giving unfavorable news about government and business less emphasis. #13 #14 CommandSequence ::= ( CommandList ) L-Systems CommandList ::= Command CommandList L-System CommandList ::= Command ::= F CommandSequence ::= ( CommandList ) Rewriting Command ::= RAngle Command ::= OCommandSequence CommandList ::= Command CommandList CommandList ::= Start: (F) Rewrite Rule: Command ::= F F → (F O(R30 F) F O(R-60 F) F) Command ::= RAngle Work like BNF replacement rules, Command ::= OCommandSequence except replace all instances at once! Why is this a better model for biological systems? #15 #16 Level 0 Level 1 Start: (F) → F (F O(R30 F) F O(R-60 F) F) Level 2 Level 3 (F) (F O(R30 F) F O(R-60 F) F) #18 Previous CS 1120 Students: The Great Lambda Tree of Ultimate Knowledge and Infinite Power Tie Dye by Bill Ingram (Level 5 with color) Rose Bush by Jacintha Henry and Rachel Kay #19 #20 Previous CS 1120 Students: PS3 - Fractals • In addition to completing the problem set, each time will submit their prettiest fractal. • The class will then vote for favorites, and the authors of the favorites will receive extra credit. • No Photoshop, etc. All PS3. – You just change the rules: → Tree Outside My Window •F (F O(R30 F) F O(R-60 F) F) ;; one fractal A Heart •F → (O(R60 F) F F O(R45 F)) ;; a new one! #21 #22 Procedure Practice Hints • Remember our strategy! • For the rest of this class, we will be practicing • Be optimistic! writing recursive procedures together. – Assume that you can write “count-fives” • Write a procedure count-fives that takes as – So the recursive case will work out input a list of numbers. It returns the number • Identify the smallest input you can solve of fives contained in its input list. – The base case – (count-fives (list 1 2 3 4 5)) -> 1 • How would you combine answers – (count-fives (list 5 -5 5 7)) -> 2 – From the current call (usually the car of the list) – (count-fives (list )) -> 0 – And the result of the recursive call – (count-fives (list 8 6 7 5 3 0 9)) -> 1 • Be creative! There are usually many solutions. #23 #24 Two versions of count-fives Liberal Arts Trivia: Medicine (define (count-fives lst) • This vector-borne infectious disease is caused (if (null? lst) by protozoan parasites. It is widespread in 0 tropical regions, such as sub-Saharan African. (if (eq? (car lst) 5) Each year there are about 515 million cases of (+ 1 (count-fives (cdr lst))) it, killing between one and three million (count-fives (cdr lst))))) people. No formal vaccine is available. Classic Both work fine! symptoms include sudden coldness followed by (define (count-fives lst) How are they rigor and then fever and sweating. (if (null? lst) 0 different? (+ (if (eq? (car lst) 5) 1 0) (count-fives (cdr lst))))) #25 #26 Liberal Arts Trivia: Accounting contains • In this bookkeeping system, each transaction is • Write a procedure contains? that takes two recorded in at least two accounts. Each arguments: an element and a list. It returns #t transaction results in one account being if the list contains the given element, #f debited and another account being credited, otherwise. with the total debits equal to the total credits. – (contains? 5 (list 1 2 3 4)) -> #f Luca Pacioli, a monk an collaborator of – (contains? 5 (list 2 3 4 5)) -> #t Leonardo da Vinci, is called the “father of – (contains? null (list 1 2 3)) -> #f accounting” because he published a usable, – (contains? 1 (list 2 null 1)) -> #t detailed description of this system. – (contains? 3 (list )) -> #f #27 #28 contains explained common-elt? • Write a procedure common-elt? that takes two (define (contains? elt lst) lists as arguments. It returns #t if there is a (if (null? lst) common element contained in both lists, #f #f otherwise. (if (eq? elt (car lst)) – (common-elt? (list 1 2 3) (list 3 4 5)) -> #t #t (contains? elt (cdr lst))))) – (common-elt? (list 1 2 3) (list 4 5 6)) -> #f – (common-elt? (list 1 2) (list 0 0 0 1)) -> #t Both work fine! (define (contains? elt lst) How are they – (common-elt? (list 1) null) -> #f (if null? lst) #f different? – (common-elt? null (list 1 2 3)) -> #f (or (eq? elt (car lst)) – (common-elt? (list 1) (list 1 2 3)) -> #t (contains? elt (cdr lst)))) #29 • Hint: contains? #30 common-elt? zero-to-hero (define (common-elt? lst1 lst2) (if (null? lst1) #f • Write a procedure zero-to-hero that takes as (if (contains? (car lst1) lst2) input a list of strings. It returns the same list in #t the same order, but every element that used (common-elt? (cdr lst1) lst2)))) to be “zero” is now “hero”. (define (common-elt? lst1 lst2) – (zero-to-hero (list “a” “zero” “b” “jercules”)) (if (or (null? lst1) (null? lst2)) #f • (“a” “hero” “b” “jercules”) (or (eq? (car lst1) (car lst2)) Both work! – (zero-to-hero (list “zorro”)) How are they • (“zorro”) (common-elt? lst1 (cdr lst2)) different? (common-elt? (cdr lst1) lst2))))) – (zero-to-hero (list “zero” “zero” “one” “zero”)) ;; this version is super slow! • (“hero” “hero” “one” “hero”) #31 #32 zero-to-hero tiny-squares • Write a procedure tiny-squares that takes as (define (zero-to-hero lst) Both work! How are they input a list of numbers. It returns a list of the (if (null? lst) null different? squares of those numbers (in the same order), (if (eq? (car lst) “zero”)) but any square above 100 is not included in the (cons “hero” (zero-to-hero (cdr lst))) output.