Answered step by step
Verified Expert Solution
Question
1 Approved Answer
5. Consider the following datatype of non-empty lists: type 'a nelist = | One of 'a | Cons of 'a * 'a nelist The constructor
5. Consider the following datatype of non-empty lists: type 'a nelist = | One of 'a | Cons of 'a * 'a nelist The constructor One is for singleton lists (One x represents the list [x]), and Cons is the cons constructor (i) Write a function ne-product : int nelist -> int that computes the product of all of the elements of a non-empty list. I 1 (ii) Write a function ne-append : 'a nelist -> 'a nelist -> 'a nelist that concatenates two non-empty lists. (iii) Write a function to list : 'a nelist -> 'a list that converts a non-empty list to the corresponding F# list. (iv) Write a function ne_map : ('a -> 'b) -> 'a nelist -> 'b nelist such that the expres- sion to_list (ne_map f xs) = List.map (to_list f xs) evaluates to true. (v) Let to_pair : 'a nelist -> 'a * 'a list be the function defined by: let to_pair xs = match xs with | One x -> (x, []) | Cons (x, xs) -> (x, to_list xs) Write a function from_pair : 'a * 'a list -> 'a nelist such that both of the expressions to-pair (from_pair (x, xs)) = (x, xs) and from_pair (to-pair ys) = ys evaluate to true. (vi) Is it possible to write a function from_list : 'a list -> 'a nelist such that the expres- sions to_list (from_list xs) = xs and from_list (to_list ys) = ys evaluate to true? Explain why. 5. Consider the following datatype of non-empty lists: type 'a nelist = | One of 'a | Cons of 'a * 'a nelist The constructor One is for singleton lists (One x represents the list [x]), and Cons is the cons constructor (i) Write a function ne-product : int nelist -> int that computes the product of all of the elements of a non-empty list. I 1 (ii) Write a function ne-append : 'a nelist -> 'a nelist -> 'a nelist that concatenates two non-empty lists. (iii) Write a function to list : 'a nelist -> 'a list that converts a non-empty list to the corresponding F# list. (iv) Write a function ne_map : ('a -> 'b) -> 'a nelist -> 'b nelist such that the expres- sion to_list (ne_map f xs) = List.map (to_list f xs) evaluates to true. (v) Let to_pair : 'a nelist -> 'a * 'a list be the function defined by: let to_pair xs = match xs with | One x -> (x, []) | Cons (x, xs) -> (x, to_list xs) Write a function from_pair : 'a * 'a list -> 'a nelist such that both of the expressions to-pair (from_pair (x, xs)) = (x, xs) and from_pair (to-pair ys) = ys evaluate to true. (vi) Is it possible to write a function from_list : 'a list -> 'a nelist such that the expres- sions to_list (from_list xs) = xs and from_list (to_list ys) = ys evaluate to true? Explain why
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