Say I want to check if a list is empty in a guard in Haskell there are two options:
length list == 0
list == []
Which of these two logical tests are more efficient? I'm inclined to say the empty list test because relies on more basic constructs rather than the prelude function length
but I'm not sure.
Get Programming with HaskellYou can't return an empty list, because an empty list is the same type as the elements of the list.
You can just use == on them directly. This means that lists instantiate Eq as long as the element type also instantiates Eq , which is the case for all types defined in the standard Prelude except functions and IO actions.
In Haskell, lists are a homogenous data structure. It stores several elements of the same type. That means that we can have a list of integers or a list of characters but we can't have a list that has a few integers and then a few characters. And now, a list!
length list == 0
needs to traverse the whole list to get its length, which means it is O(n). list == []
yields an Eq
constraint on the element type. null list
runs in constant time and has no typeclass constraints.
However, there is a neat trick to do something like length list == 0
which has the advantage that it generalizes nicely to length list1 == length list2
without going through the longer list: you can use genericLength
with a sufficiently lazy representation of natural numbers so that comparison will only force traversing the shorter of the lists.
One example is to use the Natural
type:
import Data.Number.Natural
import Data.List (genericLength)
nats :: [Int]
nats = iterate succ 0
areThereTenNats :: Bool
areThereTenNats = genericLength nats >= (10 :: Natural)
You can check if your list is empty in constant time with null list
, which returns a boolean.
Prelude> null []
True
Prelude> null [1]
False
Prelude> null ""
True
Prelude> null "Test"
False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With