listpattern-matchingsmlml

Why does `a` in `a :: nil` represent a list of length 1?


In Ullman's SML book:

Note that the way we express "list of length 1" as a pattern is to put square brackets around a single identifier, like [a]. Such a pattern can only match a list with a single element, and variable a acquires that element as its value.

Another way to express "list of length 1" is with the pattern a :: nil. Again, a acquires the lone element as its value.

In a :: nil, does a represents the head element of a list? Why does it represent a list of length 1?


Solution

  • a is the head of the list and nil is the tail. nil is a list of length zero so a :: nil has length 1.