ML module with invisible and visible components to delete the first and last columns of a matrix. The matrix is stored as a list of lists as shown below:
|4|5|6|7| |8|9|10|11| |12|13|14|15| => is 4x4 array
The matrix above will be stored as val mat=[[4,5,6,7],[8,9,10,11],[12,13,14,15]];
I need use map function.
Sample Run:
- val mat=[[4,5,6,7],[8,9,10,11],[12,13,14,15]];
- S.reduce(mat);
val it = [[5,6],[9,10],[13,14]] : int list list
But I try in different way like:
fun reduce(x,y,z,t)=(y,z);
val mat = [(4,5,6,7),(8,9,10,11),(12,13,14,15)];
map reduce(mat);
Output :
- val reduce = fn : 'a * 'b * 'c * 'd -> 'b * 'c
val mat = [(4,5,6,7),(8,9,10,11),(12,13,14,15)] : (int * int * int * int) list
val it = [(5,6),(9,10),(13,14)] : (int * int) list
How to find correct answer?
This is easier if you delete at one end first and then the other.
Removing the first column is easy; it's simply applying
List.tl
to each row:There's no library function that returns every element except the last, but it's reasonably straightforward to write:
(The case for the empty list is questionable; you probably want to treat it as an error. It's good enough for illustration purposes though...)
And then you combine the two functions:
An alternative implementation, which is quite inefficient but pleasantly symmetric, is to remove the last column by reversing the row, removing the first element, and then reversing it back:
(Nobody in their right mind would write this in actual software, but it looks nice.)