Question: Code in F# Each element of the type a listOfLists represents a lists of lists of as: InnerNil represents [[]]; SepInner(t) separates two inner lists;

Code in F#

Each element of the type a listOfLists represents a lists of lists of as: InnerNil represents [[]]; SepInner(t) separates two inner lists; ConsToInner(x, t) adds x to the beginning of the first inner list (where x : a). Every element of a listOfLists contains at least one inner list, but inner lists may be empty. If there are two or more adjacent ConsToInners, then all of their elements appear in the same inner list. Assignment1.fs implements the following four functions: convert : a list list -> a listOfLists takes a standard F# list of lists, and converts it to an element of this type (unless there has no inner lists, in which case convert raises an exception); firstInnerList : a listOfLists -> a list returns the first inner list; hasSep : a listOfLists -> bool checks whether there are at least two inner lists (in other words, whether there is at least one SepInner); afterFirstSep : a listOfLists -> a listOfLists removes the first inner list (unless there is only one, in which case it raises an exception). You have to implement three functions. (i) Implement totalLength : a listOfLists -> int, which finds the total number of a ele- ments. (Duplicates should be counted as separate elements, like List.length does.) Do this directly by recursion and pattern-matching, without using any of the four functions in the list above, and without using the any of the functions from the List module. (ii) Implement mapListOfLists : (a -> b) -> a listOfLists -> b listOfLists, which should apply the given function to each element of the list of lists (without changing the total length or the locations of the separators). Do this directly by recursion and pattern-matching, without using any of the four functions in the list above, and without using any of the functions from the List module. (iii) Implement unconvert : a listOfLists -> a list list. This should do the opposite of convert, so that convert (unconvert t) is equal to t. You can do this without (directly) pattern-matching, by using firstInnerList, hasSep, and afterFirstSep.

type 'a listOfLists =

| InnerNil

| SepInner of 'a listOfLists

| ConsToInner of 'a * 'a listOfLists

// convert : 'a list list -> 'a listOfLists

let rec convert xss =

match xss with

| [] -> failwith "Only non-empty lists are allowed"

| [[]] -> InnerNil

| []::xss -> SepInner (convert xss)

| (x::xs)::xss -> ConsToInner (x, convert (xs :: xss))

// firstInnerList : 'a listOfLists -> 'a list

let rec firstInnerList t =

match t with

| InnerNil -> []

| SepInner _ -> []

| ConsToInner (x, t) -> x::firstInnerList t

// hasSep : 'a listOfLists -> bool

let rec hasSep t =

match t with

| InnerNil -> false

| SepInner (t) -> true

| ConsToInner (x, t) -> hasSep t

// afterFirstSep : 'a listOfLists -> 'a listOfLists

let rec afterFirstSep t =

match t with

| InnerNil -> failwith "No separators"

| SepInner (t) -> t

| ConsToInner (_, t) -> afterFirstSep t

// totalLength : 'a listOfLists -> int

let rec totalLength t =

failwith "Not implemented"

// mapListOfLists : ('a -> 'b) -> 'a listOfLists -> 'b listOfLists

let rec mapListOfLists f t =

failwith "Not implemented"

// unconvert : 'a listOfLists -> 'a list list

let rec unconvert t =

failwith "Not implemented"

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!