In this module we continue to explore new, more complex, kinds of data definitions: structures, nested data, and self referential data. To assist us with designing functions that consume/produce these kinds of data we introduce constructor, destructor, and function templates. Finally, we discuss the different strategies used to design functions, and the relationship between strategies and certain kinds of data definitions.

  1. Define the term compound.
  2. Write a code example of compound data.
  3. Explain what define-struct does.
  4. List the four properties that define a value.
  5. Write code that uses a structure definition to create a new value.
  6. List the four questions used during the design of data definitions.
  7. List the three parts that make up a data definition.
  8. Write a constructor template.
  9. Explain what is a destructor template.
  10. Given a set of data definitions, write down their destructor templates.
  11. Explain the role of representation.
  12. Explain the role of interpretation.
  13. Explain the relationship between information and data.
  14. Use CamelCase for data definition names.
  15. Given a Scheme expression, write down the sequence of steps taken during evaluation.
  16. Given a function, write a contract.
  17. Given a function, write a purpose statement.
  18. Given a function, write the appropriate example.
  19. Given a Scheme function, identify the strategy used.
  20. Write a new function by composing existing functions.
  21. Explain structural decomposition.
  22. Use structural decomposition by writing a function that takes itemized, compound, or mixed data as input.
  23. Given a Scheme expression, write down the sequence of steps taken during evaluation.
  24. Explain what each step of the Design Recipe does.
  25. Write a program that uses the build-in function big-bang to create an animation.

  • DUE DATE: October 9th, 2017 @ 11:59pm

Problem 1

  1. Provide the data definition for a list of strings (los)
  2. Design the function los-total-length that consumes a list of strings and returns the sum of the length of each element in the list. For example
    
    (los-total-length (cons "a" empty))             ;; returns 1
    (los-total-length (cons "aa" (cons "a" empty))) ;; returns 3
                            
  3. Design the function los-contains that consumes a list of strings string-list and a string s and returns true if s is in string-list, otherwise returns false.
  4. Design the function los-replace-first that consumes a list of strings string-list and two strings old and new and returns the string-list but with the first old replaced by new. For example
     
    (los-replace-first (cons "a" (cons "b" (cons "a" empty))) "a" "x") 
    ;; returns 
    ;; (cons "x" (cons "b" (cons "a" empty)))
    
    (los-replace-first (cons "a" (cons "b" (cons "a" empty))) "c" "x") 
    ;; returns 
    ;; (cons "a" (cons "b" (cons "a" empty))) 
     
  5. Design the function los-replace-all that consumes a list of strings string-list and two strings old and new and returns the string-list but with all old replaced by new. For example
     
    (los-replace-all (cons "a" (cons "b" (cons "a" empty))) "a" "x") 
    ;; returns 
    ;; (cons "x" (cons "b" (cons "x" empty)))
    
    (los-replace-all (cons "a" (cons "b" (cons "a" empty))) "c" "x") 
    ;; returns 
    ;; (cons "a" (cons "b" (cons "a" empty))) 
     

Problem 2

You are helping out a team that is building software for a new cash register. The team has the following incomplete data definitions.



;; An Id is a Symbol
;; INTERP: represents a product's id 

;; A Name is a String 
;; INTERP: represents the product's name 

;; Dollars is a NonNegInteger
;; INTERP: represents the amount of dollars for a price

;; Cents is a NonNegInteger 
;; WHERE: cents is greater or equal to 0 
;;        and less or equal to 99
;; INTERP: represents the amount of cents for a price

(define-struct price [dollars cents])
;; A Price is (make-price Dollars Cents)
;; INTERP: represents total amount in dollars and cents. 

;; A Quantity is a PosInteger
;; INTERP: represent the number of items 

(define-struct line-item [id name price quantity])
;; A LineItem is a (make-line-item Id Name Price Quantity)
;; INTERP: represents a line item with the products id, name price and 
;;         quantity 
 
  1. Provide a data definition for a list of lineitems
  2. Design the function bill-total that consumes a list of lineitems and returns the total amount for the list of items.

Problem 4

A friend wants you to design a program that will help them with their daily planning and they provided the following information.

A work day consists of

  • A list of Tasks. These are the tasks that I would like to complete during the day.
  • Available work hours. This is the number of hours (whole hours) that I have available to work for the day.

A Task consists of

  • A task id. This is a unique symbol to identify the task.
  • A description. This is a sentence that describes what I need to get done for this task.
  • An estimate. The estimate is the number of hours that I think I will need to complete this task. Estimates can be on of
    • 1 hour
    • 2 hour
    • 3 hour
    • 5 hour
  1. Create data definitions to capture the information for your friend's daily planning.
  2. Design a function called free-hours that consumes a workday and returns the difference between the workday's available hours and the sum of all estimated hours for that day's tasks.
  3. Design a function called overbooked? that consumes a workday and returns false if the total number of estimated hours for all tasks in the workday is less than or equal the available hours for that workday, and true otherwise.
  4. Desgin a function called add-task that consumes a workday and a a new task and adds the task to that workday.
  5. Design a function called completed-task that consumes a workday and a task id and returns an updated workday that does not include the taks with the provided task id.
  6. Your friend would now like to plan his work week and he would like to you to design a program that will allow them to keep a list of workdays. They would also like you to design a program that given a list of workdays returns back the list of free hours, one for each workday. HINT: you might want to use your solution to part 2 of this problem.