I learn 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 causes the following warning:
! Toplevel input: ! val head::head2::tail = x ! ^^^^^^^^^^^^^^^^^ ! Warning: pattern matching is not exhaustive
I believe that a 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 considered. As far as I can tell, this function works as expected. I think the problem may be related to using []
, but I really don't know.
My question is actually three times:
- Why does sml think this is not exhaustive (how do I misinterpret it)?
- Is there a case where this function will not be executed?
- Am I doing something stupid by writing a function this way?
source share