Week 03 Lesson 02 recursive data definitions everywhere
Upcoming SlideShare
Loading in...5
×
 

Week 03 Lesson 02 recursive data definitions everywhere

on

  • 333 views

 

Statistics

Views

Total Views
333
Views on SlideShare
333
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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.

Week 03 Lesson 02 recursive data definitions everywhere Week 03 Lesson 02 recursive data definitions everywhere Presentation Transcript

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