Search code examples
wildcardsmlml

Wildcards in Standard ML


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


Solution

    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