I want to compare the length of a potentially inifinite list with a number.
Given that this doesn’t terminate:
if length [1..] < 10
then ...
else ...
I wrote this function:
-- compare (length l) n
-- (compareLengthN [1..] 10) == GT
compareLengthN :: [a] -> Int -> Ordering
compareLengthN l n
| n < 0 = GT
| null e = if length f == n then EQ else LT
| otherwise = GT
where (f, e) = splitAt n l
I would like to write this but I can’t use pattern matching with functions.
-- (length l) (operator like <, <=, ==, >=, >) n
-- (length [1..]) < 10 --> False
-- (length [1..]) == 10 --> False
-- (length [1..]) > 10 --> True
listLengthN l (>) n = GT == compareLengthN l n
listLengthN l (==) n = EQ == compareLengthN l n
listLengthN l (<) n = LT == compareLengthN l n
Suggestions?
PS: Why can’t the compiler recognize the first pattern and terminate when the list length is greater than 10? You don’t need to calculate the entire length of the list to know that: length [1..] < 10 == False