Answered step by step
Verified Expert Solution
Question
1 Approved Answer
(* main variant type definition we will use throughout the assignment *) type json = | Num of float | String of string | False
(* main variant type definition we will use throughout the assignment *)
type json =
| Num of float
| String of string
| False
| True
| Null
| Array of json list
| Object of (string * json) list
The given json is a variable and by using pattern matching techniques please answer the following thank you very much!!!
Jext, you will write 14 functions (and 2 comments!) to allow you to manipulate and process JSON values hat have already been parsed. These functions will allow us to analyze the JSON data in the next section. 5. Write a function take of type int 'a list ' a list that takes an int called n and a list called xs and returns the first n elements of x in the same order as xs. You may assume that n is non-negative and does not exceed the length of xs. Sample solution is about 5 lines. 6. Write a function firsts of type ('a ab ) list 'a list that takes a list of pairs and returns a list of all the first components of the pairs in the same order as the argument list. Sample solution is about 4 lines. 7. Write a comment in your file after your definition of firsts answering the following questions. Suppose xs has type (int * int) list, and let n be an integer between 0 and the length of xs (inclusive), and consider the expressions firsts (take (n, xs )) and take (n, firsts xs ). Either (1) write one sentence explaining in informal but precise English why these two expressions always evaluate to the same value; or (2) give example values of xs and n such that the two expressions evaluate to different values. Regardless of whether you decide option (1) or option (2) is correct, also write one sentence explaining which of the two expressions above might be faster to evaluate and why. 8. Write a function assoc of type 'a ('a 'b) list 'b option that takes two arguments k and xs. It should return Some v1 if ( k1,v1 ) is the pair in the list closest to the beginning of the list for which k and k1 are equal. If there is no such pair, assoc returns None. Sample solution is a few lines. (Note: There's a function with similar functionality in the OCaml standard library, but calling it requires features we haven't covered yet. Do not use that function or you will not receive credit.)Step 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