Search code examples
pattern-matchingsml

sml match non exhaustive warning


i want to make a function that return 0 if the list is empty otherwise 1

and the binding shoul be ( val len = fn : (int * int * int) list list list list -> int )

fun len[[[(x:(int*int*int)list)]]]=

if null x then 0 else 1


this code seems working but there is a warning 


Warning: match nonexhaustive ((x :: nil) :: nil) :: nil => ...



Solution

  • You haven't clearly specified in all cases when your function should be 0 and when it should be 1. It is easy enough to get rid of the warning. You could use a case expression which is 0 when you want it to be 0 and then be 1 in all other cases. Something like:

    fun len (nested:(int * int * int) list list list list) = case nested of
          [] => 0
        | [[]] => 0
        | [[[]]] => 0
        | [[[[]]]] => 0
        | _ => 1;