Question
Solve in Standard Meta Language of New Jersey (SML) and use only recursive definitions built up from the basic built-in functions! These functions takes a
Solve in Standard Meta Language of New Jersey (SML) and use only recursive definitions built up from the basic built-in functions!
These functions takes a 2-tuple (e.g. (N,L)) where the first element is an integer (N) and the second is a list (L). The idea is to produce a result in which the elements of the original list have been collected into lists each containing n elements (where N is the integer argument). The leftover elements (if there is any) are included as a tuple with less than N elements. Thus the type of each of these is: int * 'a list -> 'a list list.
The difference between the two functions is how they handle the left-over elements of the list when the integer doesn't divide the length of the list evenly. pairNleft treats the initial elements of the list as the extras, thus the result starts with a list of between 1 and N elements and all the remaining elements of the result list contain N elements. pairNright does the opposite: the final group contains between 1 and N elements and all the rest contain N elements. Note: these functions are not required to be tail-recursive. Examples:
> pairNleft (2,[1, 2, 3, 4, 5])
[[1], [2, 3], [4, 5]]
> pairNright (2,[1, 2, 3, 4, 5])
[[1, 2], [3, 4], [5]]
> pairNleft (3,[1, 2, 3, 4, 5])
[[1, 2], [3, 4, 5]]
> pairNright (3,[1, 2, 3, 4, 5])
[[1, 2, 3], [4, 5]]]
> pairNright (3,[]) (*this may give a warning since the type cant be inferred*)
[[]]
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