smlnj - Match non exhaustive in sml -


fun p( x::xl ) =   if x::xl = [] []   else [0]; 

it received warning: match non exhaustive.

x::xl => ...

what want is:

p( [] ) = []

when this, gives uncaught exception match [nonexhaustive match failure]

what test, x::xl = [], never true. lists algebraic types , defined as

datatype 'a list = :: of 'a * 'a list                  | [] 

meaning value list either empty list or element put in front of list.

so once initial pattern matching of x::xl has succeeded, know not empty. (this should clear, though, since assign x if x::xl empty; first element of empty list?)

you seem mixing 2 styles here, 1 being pattern matching , other being if-then-else.

fun p [] = []   | p (_::_) = [0]  fun p xs = if list.null xs [] else [0]