Test whether a list is empty.
null tests whether a Foldable structure is empty, returning True if it contains no elements and False otherwise. Its generalized type signature is Foldable t => t a -> Bool. For lists, it checks if the list is [].
null is the preferred way to check for emptiness because it runs in O(1) time for listsβit only needs to inspect the outermost constructor. This is much more efficient than length xs == 0, which traverses the entire list. null also works safely on infinite lists, unlike length.
Beyond lists, null works on any Foldable instance. For example, null Nothing returns True, null (Just 5) returns False, and null (Data.Map.empty) returns True. Using null consistently across different container types leads to more generic and reusable code.
| Name | Description | Optional |
|---|---|---|
xs |
A Foldable structure (commonly a list [a]) to test for emptiness. | No |
null [] -- True
null [1, 2, 3] -- False
null "" -- True