Lab 4: Mutually Recursive Data
Partners
Work on this lab with someone different from your homework partner and, if possible, someone you haven’t worked on any other lab with.
Teachpack
This lab relies on the ability to retrieve text information from the network (and files) into ISL data. This design task is beyond the design skills you have encountered in this class, so we provide a teachpack that does it for you.
Please save this teachpack into the same folder as your lab. Feel free to check out the source code of the teachpack. It’s written in full Racket, but I bet you’ll be surprised by how much you can actually understand!
Put
(version 2)
(require "teachpack.rkt")
at the top of your file.
JSON
; A JSON is one of:
; - String
; - Number
; - Boolean
; - 'null
; - Array
; - Hash
; An Array is a [Listof JSON]
; This represents a collection of data, essentially a list
(define-struct hash (data))
; A Hash is a (make-hash HashContent)
; This represents a named set of data, similar to a structure
; A HashContent is a [Listof KeyValuePair]
; This represents the set of key-value pairs contained in a Hash
(define-struct kv-pair (key value))
; A KeyValuePair is a (make-kv-pair String JSON)
; This represents an entry into a Hash
1. Develop three examples of a JSON.
2. Write out the template for a JSON. Remember the guideline, One Function Per JSON Definition! Don’t forget to apply all templates on non-atomic data.
Now that you have the template, do you see why this is considered mutually recursive?
Writing functions on mutually recursive data
3. Design the function render-data which consumes a JSON and renders it as String according to the following rules:
- Numbers are displayed as plain numbers
- Boolean are displayed as true or false
- ‘null is displayed as null
- Strings are wrapped in double quotes (")
- Arrays are wrapped in square brackets ([])
- Elements in Arrays are separated by a comma and a space
- Hashes are wrapped in curly brackets ({})
- Hashes’ keys are wrapped in double quotes
- Hashes’ key-value pairs are separated by a comma and a space
- KeyValuePairs’ keys and values are separated by a colon and a space
Your solution may or may not include trailing commas.
Given:
(make-hash (list (make-kv-pair "foo" 12)
(make-kv-pair "bar" (list -12 'null))
(make-kv-pair "baz" (list "string" #true))))
Expected:
"{\"foo\": 12, \"bar\": [-12, null, ], \"baz\": [\"string\", true, ], }"
Writing Functions on Pokemon Data
The Pokémon portal provides all kinds of helpful data about video game creatures known as Pokémon. Using our data representation, we can send requests to this portal and get back some real data to play around with.
The teachpack includes the get-pokemon-data function, which
- consumes a pokédex number (To get the pokédex numbers of Pokémon, look at the complete list. The number you’ll want to use is the Ndex number. Note The server may not have data on the most recent generation of Pokémon, so if you try to request one of those, then you may get an error.)
- makes a remote-function call to the Pokémon portal, and
- returns information about that Pokémon represented as JSON.
Example: Copy and paste this definition into drracket and hit “run”:
(define CHESPIN (get-pokemon-data 650))
If you look at the value of CHESPIN, you will notice that many keys of the fields in the data response say “url” and come with a URI. Each of these keys comes with String that represents a URI. These URIs point to another JSON with information about this part of the pokémon description. Eventually we wish to replace these key-value pairs with their JSON descriptions.
4. Design more-info
. The function takes in a JSON on a Pokémon and a function from String to String. It replaces the “url” key-value pair with the key “more_info” and the value obtained by using the provided function to get data at the original “url” value.
The value next to “url” is guaranteed to be a String and a well-formed URI. If the value for a “url” key points to a Pokémon, that is, the string contains (hint string-contains?
) the word “/pokemon/”, there’s no need to fill in this information.
5. Modify the function more-info
so that if the replacement value for a “url” key points to a Pokémon, there’s no need to fill in this information any further.
To retrieve the information for “url” keys from the “interweb”, the teachpack provides get-url
, which,
- consume a String (which represents a URI of the form “/api/v1/[SOME THING]/[SOME NUMBER/]”),
- makes a function call to the portal using that URL, and
- returns a String that represents the returned information.
6. Once you have finished the precedinng exercise, you can apply more-info
to the library-provided get-url
function. This will take some time to run since it has to make a bunch of internet requests.
Bonus Questions
7. Write a function that counts the number of 1st Generation sprites for different Pokemon.
8. Write a function that takes in two Pokemon and compares the stats to predict who will win in a fight.