Wildcards in Standard ML

981 views Asked by At

I just came across the following function in ML for the Working Programmer:

fun null [] = true
    | null (_::_) = false

1) Can't both wildcards be empty lists? If not, how does ML prevent this?

2) Could the function be shortened to be:

fun null [] = true 
    | false

Why / why not?

Thanks for the help, bclayman

1

There are 1 answers

2
IonuČ› G. Stan On BEST ANSWER
  1. Yes, they can, but the matched list won't be empty, so the result of the null function holds, i.e., [] :: [], which is equivalent to [[]], is not an empty list.

  2. No, that's syntactically invalid. However, it can be shortened to this:

fun null [] = true 
  | null _ = false