Assignment 16
Due Date Thursday 03/28 at 9pm
Purpose To get more practice with mutually recursive 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 11 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.
Working with mutually recursive data
; An IceCream is one of: ; - String (representing a single ice cream flavor) ; - Swirl (representing two ice creams swirled together) ; - Sundae (representing an ice cream sundae) (define-struct swirl [type1 type2]) ; A Swirl is a (make-swirl IceCream IceCream) ; - where type1 represents the first type of ice cream to swirl ; - and type2 represents the second type of ice cream to swirl (define-struct stack [scoops]) (define-struct topping [type ic]) ; A Sundae is one of: ; - (make-stack [List-of IceCream]) ; representing a stack of ice cream scoops ; - (make-topping String IceCream) ; representing a topping on top of some ice cream
Exercise 1 Complete the steps of the data design recipe for all the data definitions given above.
Exercise 2 Design the function count-flavors which, given an IceCream, produces the number of flavors in the ice cream. If a flavor appears more than once you may count it multiple times. Remember: A single function should only deal with a single data type.
Exercise 3 Design the function flavor-exists? which, given an IceCream and a String representing a certain flavor, returns #true if the given IceCream contains that flavor. Remember: A single function should only deal with a single data type.
Exercise 4 Design the function remove-topping which, given an IceCream and a String representing a topping, produces the IceCream without any of that topping included.
JSON
JavaScript Object Notation (JSON) is a lightweight data interchange format that is widely used on the web and on mobile applications. JSON can represent basic data types such as numbers, strings, and booleans, as well as lists of items and "objects" (collections key-value pairs). In brief, numbers are represented as numbers, strings are enclosed by double-quotes, and booleans are represented as true and false. A list of numbers would be represented in JSON as
[1, 2, 4, 19]
a single object representing a coffee order might be written as
{"order": "espresso", "cost": 4, "time": "9:21am", "completed": true}
and a list of objects representing professors might be represented as
[{"name": "Alan Mislove", "nuid": 12345, "courses": [{"course": "CS2500", "semester": "Fall 2018"}, {"course": "CS2500", "semester": "Spring 2019"}]}, {"name": "Ben Lerner", "nuid": 67890, "courses": [{"course": "CS2500", "semester": "Fall 2017"}]}]
where each professor internally has a list of objects under the key courses.
Now, consider a representation of JSON objects in DrRacket using the following data definitions:
; A JSONValue is one of: ; - Integer ; - String (that does not contain double-quotes ["]) ; - Boolean ; - JSONObject ; - JSONList ; A JSONList is a [List-of JSONValue] ; Interpretation: A list of values (define-struct object [key value next]) ; A JSONObject is one of: ; - (make-object String JSONValue '()) ; - (make-object String JSONValue JSONObject) ; Interpretation: A set of key-value pairs, where the ; first option represents an object with only a single ; key-value pair.
Exercise 5 Design the function jsonify that accepts a JSONValue and returns a JSON-formatted String that represents the given value. In other words, jsonify will serialize the given JSONValue into a JSON-formatted String.
Important note: In Racket, the double-quote character (") is used to define Strings. If you want your string to actually contain a double-quote, you need to write it with a backslash: \". Thus, the if you want to represent foo"bar in Racket, you would write "foo\"bar". You will also notice that DrRacket will print out Strings containing double-quote characters in the same manner.
To aid you in understanding this problem, you might find the following example helpful:
(check-expect (jsonify (make-object "foo" (list 1 "nat") (make-object "bar" (make-object "baz" #true '()) (make-object "blah" 3 '())))) "{\"foo\": [1, \"nat\"], \"bar\": {\"baz\": true}, \"blah\": 3}")