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.
- Define the term compound.
- Write a code example of compound data.
- Explain what define-struct does.
- List the four properties that define a value.
- Write code that uses a structure definition to create a new value.
- List the four questions used during the design of data definitions.
- List the three parts that make up a data definition.
- Write a constructor template.
- Explain what is a destructor template.
- Given a set of data definitions, write down their destructor templates.
- Explain the role of representation.
- Explain the role of interpretation.
- Explain the relationship between information and data.
- Use CamelCase for data definition names.
- Given a Scheme expression, write down the sequence of steps taken during evaluation.
- Given a function, write a contract.
- Given a function, write a purpose statement.
- Given a function, write the appropriate example.
- Given a Scheme function, identify the strategy used.
- Write a new function by composing existing functions.
- Explain structural decomposition.
- Use structural decomposition by writing a function that takes itemized, compound, or mixed data as input.
- Given a Scheme expression, write down the sequence of steps taken during evaluation.
- Explain what each step of the Design Recipe does.
- Write a program that uses the build-in function big-bang to create an animation.
- DUE DATE: October 9th, 2017 @ 11:59pm
Problem 1
- Provide the data definition for a list of strings (los)
-
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
-
Design the function
los-contains
that consumes a list of stringsstring-list
and a strings
and returns true ifs
is instring-list
, otherwise returns false. -
Design the function
los-replace-first
that consumes a list of stringsstring-list
and two stringsold
andnew
and returns thestring-list
but with the firstold
replaced bynew
. 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)))
-
Design the function
los-replace-all
that consumes a list of stringsstring-list
and two stringsold
andnew
and returns thestring-list
but with allold
replaced bynew
. 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
-
Provide a data definition for a list of
lineitem
s -
Design the function
bill-total
that consumes a list oflineitem
s 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
- Create data definitions to capture the information for your friend's daily planning.
-
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. -
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. -
Desgin a function called
add-task
that consumes a workday and a a new task and adds the task to that workday. -
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. - 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.