问题
I am trying to write a function to delete a list from another list.
''a list -> ''a list -> ''a list
Here's what I have so far:
fun delete _ [] = [] |
delete (h1::t1) (h2::t2) =
if h1=h2
then t2
else h2::delete (h1::t1) t2;
I am using MoscowML and it gives me a Warning: pattern matching is not exhaustive error.
A test of the above function:
- delete [4,5] [1,2,3,4,5,6,7,8];
> val it = [1,2,3,5,6,7,8] : int list
The desired output is:
> val it = [1,2,3,6,7,8] : int list
回答1:
There are two issues here:
1- Why is the interpreter raising the Warning: pattern matching is not exhaustive error
2- What can be done to make the code working.
Concerning the first point, the reason for the warning is because that you are not checking every possibility that may occur. The function delete
as it currently stands checks only for two possibilities:
-1 The second list being the empty list (covered by the pattern: _ [] =
)
-2 Both lists not being empty (covered by the second pattern: (h1::t1) (h2::t2) =
)
However, there is a third possibility, namely the first list being the empty list. Therefore, the following input would result in an error: delete [] [1,2,3,4,5,6]
Concerning the second point, if the exact requirement is to delete from the second list the elements of the first list in succession and only once, then your solution is very close. The else
branch is fine, only the then
branch needs more attention.
By correcting the then
branch I get following results:
delete [4,5] [1,2,3,4,5,6,7,8] = [1,2,3,6,7,8];
delete [5,4] [1,2,3,4,5,6,7,8] = [1,2,3,4,6,7,8];
delete [4,4,5] [1,2,3,4,5,6,7,8] = [1,2,3,5,6,7,8];
delete [4,5,6] [1,2,3,4,5,6,7,8] = [1,2,3,7,8];
delete [4,6,5] [1,2,3,4,5,6,7,8] = [1,2,3,5,7,8];
delete [4,6,5] [1,2,3,4,6,7,8,5] = [1,2,3,7,8];
If however you want to delete all the elements of the first list occurring in the second list regardless of their order, then you will need to rethink your approach.
for instance, if you want the following result:
delete [4,6,5] [1,2,3,4,4,5,5,5,4,4,6,6,5,5,6,6,6,6,6,7,8,5] = [1,2,3,7,8];
then you need to do it in two steps:
First write a function del
that given one element will delete all its occurrences in a list: fun del e l = ...
The implementation thereof is practically identical to the one you provided for delete
, except you will need to change the then
branch slightly.
After you have del
, now you can implement the function delete
that given a list, it will delete all the occurrences of that list in the second list. Here you will use the previously defined function del
.
来源:https://stackoverflow.com/questions/32930694/sml-list-deletion