Upcoming SlideShare
×

# Lesson 04 Lists Of Lists Of Lists

721 views
643 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
721
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
2
0
Likes
0
Embeds 0
No embeds

No notes for slide
• Welcome to Week 3, Lesson 4: Arbitrarily Nested ListsSo far we&apos;ve dealt with structures, structures with lists in them, and lists of structures.Now let&apos;s look at lists of lists.
• ### Lesson 04 Lists Of Lists Of Lists

1. 1. Lists of Lists of Lists of ...<br />CS 5010 Program Design Paradigms “Bootcamp”<br />Week 03, Lesson 4<br />TexPoint fonts used in EMF. <br />Read the TexPoint manual before you delete this box.: AAA<br />1<br />
2. 2. Lists of Lists of ... strings<br />"alice"<br />"bob"<br />"carole"<br />("alice" "bob")<br />(("alice" "bob") "carole")<br />("alice" <br />(("alice" "bob") "dave")<br />"eve") <br />
3. 3. Data Definition<br />An S-expression of Strings (SoS) is either<br />-- a String<br />-- a List of SoS's<br />A List of SoS's (LoSS) is either<br />-- empty<br />-- (cons SoSLoSS)<br />
4. 4. This is mutual recursion<br />SoSLoSS<br />defined in terms of <br />defined in terms of <br />
5. 5. Mutually Recursive Data Definitions<br />One task, one function<br />So: one function per data definition<br />Here, functions come in pairs<br />Write the contracts and purpose statements together, or<br />Write one, and the other one will appear as a wishlist function<br />
6. 6. Template: functions come in pairs<br />;; sos-fn: SoS-> ??<br />(define (sos-fns)<br /> (cond<br /> [(string? s) ...]<br /> [else (loss-fn s)]))<br />;; loss-fn: LoSS-> ??<br />(define (loss-fn los)<br /> (cond<br /> [(empty? los) ...]<br /> [else (... (sos-fn(first los))<br /> (loss-fn(rest los)))]))<br />
7. 7. occurs-in?<br />;; occurs-in? : Sos String -> Boolean<br />;; returns true if the given string occurs somewhere in the given sos.<br />;; occurs-in-loss? : Loss String -> Boolean<br />;; returns true if the given string occurs somewhere in the given loss.<br />
8. 8. Examples/Tests<br />(check-expect (occurs-in? "alice" "alice") true)<br />(check-expect (occurs-in? "bob" "alice") false)<br />(check-expect <br /> (occurs-in? (list "alice" "bob") "cathy") <br /> false)<br />(check-expect <br /> (occurs-in? <br /> (list<br /> (list "alice" "bob") <br /> "carole") <br /> "bob") <br /> true)<br />(check-expect <br /> (occurs-in? <br /> (list "alice" <br /> (list (list "alice" "bob") "dave") <br /> "eve")<br /> "bob")<br /> true)<br />See book re list vs. cons<br />
9. 9. The Code<br />(define (occurs-in? sosstr)<br /> (cond<br /> [(string? sos) (string=? sosstr)]<br /> [else (occurs-in-loss? sosstr)]))<br />(define (occurs-in-loss? loss str)<br /> (cond<br /> [(empty? loss) false]<br /> [else (or (occurs-in? (first loss) str)<br /> (occurs-in-loss? (rest loss) str))]))<br />
10. 10. Finger Exercises<br />;; number-of-strings : Sos -> Number<br />;; number-of-strings-in-loss : Loss -> Number<br />;; returns the number of strings in the given sos or loss.<br />;; characters-in : Sos -> Number<br />;; characters-in-loss : Loss -> Number<br />;; returns the total number of characters in the strings in the given sos or loss.<br />
11. 11. Summary<br />Trees of structures occur all the time<br />Mutually recursive data definitions<br />Mutual recursion in the data definition leads to mutual recursion in the template<br />Mutual recursion in the template leads to mutual recursion in the code<br />Study the tests: <br />always ask: how could a program pass the tests and still be wrong?<br />always ask: how could a program fail the tests and still be correct?<br />