Use
compareLength xs n as a safer and faster
alternative to
compare (
length xs)
n. Similarly, it's better to write
compareLength xs 10 ==
LT instead of
length xs < 10.
While
length would force and traverse the entire spine of
xs (which could even diverge if
xs is infinite),
compareLength traverses at most
n elements to
determine its result.
>>> compareLength [] 0
EQ
>>> compareLength [] 1
LT
>>> compareLength ['a'] 1
EQ
>>> compareLength ['a', 'b'] 1
GT
>>> compareLength [0..] 100
GT
>>> compareLength undefined (-1)
GT
>>> compareLength ('a' : undefined) 0
GT