null

A predicate that tells you whether something is empty is conventionally named null.

You can find the following function in the Prelude module: null in Prelude

What things are Foldable? Foldable One example is lists. null can tell you whether a list is empty.

String is a type alias for [Char], so null can also tell you whether a string is the empty string.

The expression null xs is usually equivalent to length xs == 0. But null may be much faster. Consider what happens if the list is quite large; length must evaluate the entire list, so it takes a while. But null doesn’t need to go any further than the first element to figure out that a list is not empty.

Remember also that lists can be infinite, which means that the length approach may not even work at all. null will, though.

Plenty of other “collection”-like types have Foldable instances, Set, Map, HashSet, HashMap, Vector not just lists. So null also works with Set, Map, HashSet, HashMap, Vector, etc. All of these modules also define their own null functions, so you can use a less-polymorphic version instead of the method from Foldable if you like:

There are some types Text, ByteString like Text and ByteString that aren’t “collections” in this sense, because they don’t have a type parameter, but are still sort of list-like. Text is conceptually a list of characters, and ByteString is conceptually a list of Word8. A Word8 is a byte. These modules too define their own functions called null that follow this pattern.

There are a few classes outside of base that generalize these kinds of parameterless collections.

Whereas collections whose element type is parameterized belong to the Foldable class, collections that only work with one MonoFoldable is defined in the mono-traversable package. specific type of element belong to the MonoFoldable class. A type family called Element specifies what kind of elements each of these collections holds. The functions in the mono-traversable package are all prefixed with the letter o to distinguish them from similar-named things in Prelude, so the emptiness test in MonoFoldable is called onull.

The ListLike package ListLike is the name of both the class and the package. also defines a class with a null method. It takes a different approach to handling monomorphic collections, using a multi-parameter typeclass instead of a type family. The ListLike class is more specific than Foldable and MonoFoldable; it only pertains to things that have a linear structure, like lists and Vectors. Types like Map are not list-like in this way.The ListLike documentation points out that technically Map could have a ListLike instance, but it behaves oddly.

Join Type Classes for courses and projects to get you started and make you an expert in FP with Haskell.