Assignment 7
Due Date Thursday 02/07 at 9pm
Purpose To design functions over unions and self-referential data.
You should submit a single .rkt file containing your responses to all exercises via the Handin Server. We accept NO email submissions. Failure to submit a .rkt file will result in a 0.
You are only allowed to use the language specified at the top of this page: failure to do so will result in a 0.
Your code MUST conform to the guidelines outlined in the style guide on the course website. The style guide will be updated as the semester progresses so please remember to read it before submitting each assignment.
You must follow all the steps of the design recipe when completing this assignment.
Please be sure to look at the feedback for assignment 5 before submitting, as we will be grading you more harshly on things we have warned you about before.
You must submit this assignment with your partner. Please make sure you can submit with your partner before 5pm on Thursday or we cannot guarantee that you will be able to submit your homework before the deadline. If the course staff are unable to assist you after 5pm and this causes your homework to be late we will not grant an extension.
You should not use cons or list for this assignment. You may only use the topics we have covered in lecture.
Exercise 1 Consider the data definition below:
(define-struct monkey [name c others]) ; A MonkeyChain is a (make-monkey String String MonkeyChain) ; ; Interpretation: A collection of monkeys ; - name is the name of this monkey ; - c is the color of this monkey ; - others is the other monkeys (or barrel) it is attached to In a comment, describe the problem with this data definition.
Exercise 2 Consider the data definition below:
(define-struct database [entry others]) ; A StudentDatabase is one of: ; - "empty database" ; - (make-database StudentEntry StudentDatabase) ; and represents either a database with no information (if it's a String) or a database with at ; least one entry with information about a student (define-struct entry [name nuid gradyear]) ; A StudentEntry is a (make-entry String Nat Nat) ; - where name is the student's full name ; - nuid is the student's NUID # ; - and gradyear is the student's year of graduation. Write the template for a StudentDatabase. If you need help writing this template please refer to the template questions on the design recipe page.
Exercise 3 Consider the following alternative data definition for a StudentDatabase:
(define-struct database [name nuid gradyear others]) ; A StudentDatabase is one of: ; - "empty database" ; - (make-database String Nat Nat StudentDatabase) ; and represents either a database with no information (if it's a String) or a database with at ; least one entry with information about a student (their full name, NUID #, and year of ; graduation) Which definition has the better design? Why? Write your answers in a comment.
Exercise 4 Design a data definition for a website of real estate listings. The site can have any number of real estate listings (even zero). Each listing should have an address with a house number, street name, and zip code. Each listing should also have a price for the house, a number of bedrooms, and a number of bathrooms.
Exercise 5 We want to design a function which counts how many students in a database are graduating this year. A student has written the signature and purpose below. In a comment, describe any problems you see with the signature and purpose.
; num-students : StudentDatabase -> Nat ; Takes in a StudentDatabase and produces a Nat representing the number of students graduating ; this year
Exercise 6 After writing their signature and purpose, the student starts writing their function. In a comment, write any problems you find in the code below. You may assume the student is using the first data definition given, and that this choice is not the problem we are looking for. In addition, provide a comprehensive set of tests that the student should have written before they started coding. You should NOT copy the code into your file, so you will need to comment out the tests in order for your file to run properly.
(define (num-students db) (cond [(string? db) 0] [(database? db) (if (= (entry-gradyear (database-entry db)) 2019) (add1 (num-students (database-others db))) (num-students (database-others db)))]))
Exercise 7 Consider the data definition for a slideshow, given below: Generally speaking we try to avoid using Images in our data definitions. That’s because they give us almost no information to manipulate. All we can do is display them to the user. We can’t find out what’s in them or remove any parts of them. However, we will just ignore this problem for now as it is not the focus of this exercise.
(define-struct slideshow [title slides]) ; A SlideShow is a (make-slideshow String SlideSet) ; - where title is the slideshow's title ; - and slides is the set of slides that will be shown (define-struct sset [slide1 other-slides]) ; A SlideSet is one of: ; - "no more slides!" ; - (make-sset Image SlideSet) ; and represents either no slides (the String) or a set containing at least one slide We want to design a function that takes in a SlideShow and shows all the slides next to each other. A student has jumped right to the "good part" and started coding:
(define (display-all-slides sshow) (cond [(string? (slideshow-slides sshow)) empty-image] [(sset? (slideshow-slides sshow)) (beside (sset-slide1 (slideshow-slides sshow)) (display-all-slides (sset-other-slides (slideshow-slides sshow))))])) In a comment, describe any problems you see in this code. In addition, complete the steps of the design recipe (for both data and functions) that the student skipped. You should NOT copy the code into your program. Because of this you will need to comment out the tests in order for your file to run properly.
What is interesting about this assignment? Unlike previous assignments we have not asked you to define any constants or functions - there is almost nothing you can run to see if it works. Why is that? In this course we are not trying to teach you how to code in BSL. You could learn that on your own. In this course we are trying to teach you how to DESIGN. Because of that the most important things are the non-code parts - signatures, purpose statements, tests. Finding out whether something "works" is not interesting to us. What is interesting is finding out whether something is well designed.
Why do we care so much about design? More often than not, code is not something you write and never look at again. It is something that you, and probably many other people, will come back to again and again. You will need to debug it, update it, and revise it as requirements change. Without proper design guiding someone, this endeavor becomes massively more difficult. In addition, as a nice side benefit, designing your code cleanly often makes it easier to create code that works too!