0
Upcoming SlideShare
×

# Week 03 Lesson 02 recursive data definitions everywhere

205

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
205
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
1
0
Likes
0
Embeds 0
No embeds

No notes for slide
• Welcome to Week 3, Lesson 2: Recursive Data Definitions EverywhereIn this lesson, we will see some of the many ways in which recursive data definitions can arise.
• ### Transcript of "Week 03 Lesson 02 recursive data definitions everywhere"

1. 1. Recursive Data Definitions Everywhere<br />CS 5010 Program Design Paradigms “Bootcamp”<br />Week 03, Lesson 2<br />TexPoint fonts used in EMF. <br />Read the TexPoint manual before you delete this box.: AAA<br />1<br />
2. 2. Lists of Structures<br />(define-struct payroll-entry <br /> (name ssn position <br /> current-hrs-worked pay-rate current-pay ytd-pay))<br />;; A PayrollEntry is a <br />;; (make-payroll-entry <br />;; String Number String <br />;; Number NumberNumberNumber)<br />;; where:<br />;; name is the employee's name<br />;; ssn is the employee's Social Security Number<br />;; position is the the employee's position<br />;; current-hrs-worked is the number of hours the employee has worked <br />;; in the current pay period<br />;; pay-rate is the employee's pay in \$/hr<br />;; current-pay is the amount the employee is to be paid<br />;; for the current pay period<br />;; ytd-pay is the employee's total pay for the current year<br />;; through the current pay period.<br />
3. 3. Examples<br />(define entry1 (make-payroll-entry <br /> "Joe" 123456789 "teaching assistant"<br /> 0 20.00 0 1000))<br />(define entry2 (make-payroll-entry<br /> "Mitch" 987654321 "professor"<br /> 10 100.00 1000. 10000.))<br />
4. 4. ;; update-entry : PayRollEntry Number -> PayRollEntry<br />;; produces a payroll entry like the given one, <br />;; except that current-hrs-worked is updated to the given <br />;; number, and current-pay and ytd-pay are updated<br />;; accordingly.<br />;; example:<br />;(update-entry entry1 10) <br />;= (make-payroll-entry<br />; "Joe" 123456789 "teaching assistant"<br />; 200 20.00 400.0 1400)<br />update-entry<br />
5. 5. update-entry (cont’d)<br />;; Design Strategy: structural decomposition on entry [PayrollEntry]<br />(define (update-entry entry hrs)<br /> (make-payroll-entry<br /> (payroll-entry-name entry)<br /> (payroll-entry-ssn entry)<br /> (payroll-entry-position entry)<br /> hrs<br /> (payroll-entry-pay-rate entry)<br /> (* hrs (payroll-entry-pay-rate entry)) ; the pay for ; this period<br /> (+ (payroll-entry-ytd-pay entry)<br /> (* hrs (payroll-entry-pay-rate entry)))))<br />
6. 6. Exercise:<br />Observe that<br /> (* hrs (payroll-entry-pay-rate entry))<br />occurs twice in the code. What information does this quantity represent?<br />Design a function to compute this.<br />Rewrite update-entry to use your new function. <br />
7. 7. But there are many people in our company!<br />;; A ListOfPayrollEntries (LoPE) is one of<br />;; -- empty<br />;; -- (cons PayrollEntryLoPE)<br />;; A Payroll is a LoPE<br />
8. 8. Exercise:<br />;; raise-all : Payroll * Number -> Payroll<br />;; Produces a payroll like the given payroll, but with everyone's<br />;; pay rate increased by the given number.<br />;(raise-all (list entry1 entry2) 0.10)<br />;= (list<br />; (make-payroll-entry <br />; "Joe" 123456789 "teaching assistant"<br />; 0 22.00 0 1000)<br />; (make-payroll-entry<br />; "Mitch" 987654321 "professor"<br />; 10 110.00 1100. 10000.))<br />What does the example show us that isn’t obvious from the purpose statement?<br />
9. 9. Exercise:<br />;; rename-position : Payroll * String * String<br />;; Given a payroll, an old position name, and a new position name,<br />;; returns a payroll like the given one, except all entries matching <br />;; the old position name are updated.<br />
10. 10. An Example/Test<br />;(rename-position <br />; (list entry1 entry2)<br />; "teaching assistant"<br />; "grader")<br />;= (list<br />; (make-payroll-entry <br />; "Joe" 123456789 "teaching assistant"<br />; 0 20.00 0 1000)<br />; (make-payroll-entry<br />; "Mitch" 987654321 "professor"<br />; 10 100.00 1000. 10000.))<br />This test is insufficient! How could you write an incorrect function that would still pass this test?<br />
11. 11. Summary<br />Lists of structures occur all the time<br />Important to have interpretation in the data defs!<br />The recipe is your friend!<br />Study the tests: <br />always ask: how could a program pass the tests and still be wrong?<br />
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.