I'm learning sml, and wrote the following simple function:
(* Return a list with every other element of the input list *)
fun everyOther [] = []
| everyOther [x] = [x]
| everyOther x = let
val head::head2::tail = x
in
head::everyOther(tail)
end;
Which is generating the following warning:
! Toplevel input:
! val head::head2::tail = x
! ^^^^^^^^^^^^^^^^^
! Warning: pattern matching is not exhaustive
I believe the function can never fail, since val head::head2::tail
will always work for lists with two or more elements and the case of one element and zero elements is covered. As far as I can tell, this function works as expected. I think the issue might be related to the use of []
but I really don't know.
My question is actually three fold:
SML gives you that warning because it doesn't know that x
has at least two elements. All it knows is that x
is a list, it doesn't remember the fact that x
had to have not matched the first two patterns, to go into the third case.
No, the code can not fail.
There is no reason to perform the pattern match in the let-statement. You can just put the pattern to the fun
statement, which will result in less code and remove the warning:
fun everyOther [] = []
| everyOther [x] = [x]
| everyOther (head::head2::tail) = head :: everyOther tail;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With