Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please answer using ML, thank you in advance for the help! 10. Write a function dots that takes a json called j and a string
Please answer using ML, thank you in advance for the help!
10. Write a function dots that takes a json called j and a string list called fs that represents an access path, or in other words, a list of field names. The function dots returns a json option by recursively accessing the fields in fs, starting at the beginning of the list. If any of the field accesses occur on non-objects, or to fields that do not exist, return None. Otherwise, return Some v where v is the value of the field "pointed to by the access path. (Hint: Use recursion on fs plus your solution to the previous problem.) Sample solution is about 7 lines. 11. Write a function one_fields that takes a json and returns a string list. If the argument is an object, then return a list holding all of its field names (not field contents). Else return the empty list. Use a tail-recursive, locally defined helper function. The list you return can be in any order, but it is probably easiest to have the results in reverse order from how they appear in the object, and this reverse order is fine/expected. Sample solution is about 10 lines. 12. Write a function no_repeats that takes a string list and returns a bool that is true if and only if no string appears more than once in the input. Do not (!) use any explicit recursion. Rather, use provided helper function dedup (which returns its argument without duplicates) together with standard library function List.length to complete this problem in one line. 13. Write a function recursive_no_field_repeats that takes a json and returns a bool that is true if and only if no object anywhere "inside (arbitrarily nested) the json argument has repeated field names. (Notice the proper answer for a json value like False is true. Also note that it is not relevant that different objects may have field names in common.) In addition to using some of your previous functions, you will want two locally defined helper functions for processing the elements of a JSON array and the contents of a JSON object's fields. By defining these helper functions locally, rather than at the top level, they can call recursive_no_field_repeats in addition to calling themselves recursively. Sample solution is about 15 lines. 10. Write a function dots that takes a json called j and a string list called fs that represents an access path, or in other words, a list of field names. The function dots returns a json option by recursively accessing the fields in fs, starting at the beginning of the list. If any of the field accesses occur on non-objects, or to fields that do not exist, return None. Otherwise, return Some v where v is the value of the field "pointed to by the access path. (Hint: Use recursion on fs plus your solution to the previous problem.) Sample solution is about 7 lines. 11. Write a function one_fields that takes a json and returns a string list. If the argument is an object, then return a list holding all of its field names (not field contents). Else return the empty list. Use a tail-recursive, locally defined helper function. The list you return can be in any order, but it is probably easiest to have the results in reverse order from how they appear in the object, and this reverse order is fine/expected. Sample solution is about 10 lines. 12. Write a function no_repeats that takes a string list and returns a bool that is true if and only if no string appears more than once in the input. Do not (!) use any explicit recursion. Rather, use provided helper function dedup (which returns its argument without duplicates) together with standard library function List.length to complete this problem in one line. 13. Write a function recursive_no_field_repeats that takes a json and returns a bool that is true if and only if no object anywhere "inside (arbitrarily nested) the json argument has repeated field names. (Notice the proper answer for a json value like False is true. Also note that it is not relevant that different objects may have field names in common.) In addition to using some of your previous functions, you will want two locally defined helper functions for processing the elements of a JSON array and the contents of a JSON object's fields. By defining these helper functions locally, rather than at the top level, they can call recursive_no_field_repeats in addition to calling themselves recursively. Sample solution is about 15 linesStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started