Skip to content

null and length

The two simplest questions we can ask about a list is whether it is empty and what its length is. null and length are the functions we can use to do this. Here are their definitions:

null :: [a] -> Bool
null [] = True
null _  = False
length :: [a] -> Int
length = go 0
  where
    go len []     = len
    go len (_:xs) = go (len + 1) xs

The following definition of length would have been more natural, but the one just given has the advantage of being tail-recursive.

length []     = 0
length (_:xs) = 1 + length xs