I am reading through the "Starting Out" chapter of Learn You a Haskell for Great Good!. It says:
null
checks if a list is empty. If it is, it returnsTrue
, otherwise it returnsFalse
. Use this function instead ofxs == []
(if you have a list calledxs
)
I tried in ghci:
xs = [] -- and then,
xs == []
null xs
Both of them are True
.
I wonder what's the difference.
Should I use the null
function instead of == []
and why?
Best Answer
You should use
null
. In most cases it doesn't matter, but it is a good habit to get into anyway, because occasionally you may want to check if a list of non-comparable things is empty. Here is a short, crisp example showing this difference: