Am Samstag 27 Februar 2010 15:41:29 schrieb Tom Davie:
> While the advice here is sound 90% of the time, it's evidence of falling
> into the trap of underestimating lazyness. If 0 and length list are
> both of type Nat, the first will be exactly as fast as the second.
>> Bob
As it is, we have
length :: [a] -> Int
Of course, an implementation with a lazy Int type is thinkable, but for the
time being, it's a pretty safe bet that Int comparisons need complete
evaluation of the arguments. (In GHC, you can use rewrite rules to avoid
it, but if you think of that, you wouldn't write "length xs == k" unless
you really mean it in the first place.)
Most of the uses of "length xs == k" are due to the fact that people are
used to strict list-like structures carrying their length with them and not
to singly linked lazy lists of unknown length, I believe.
So I'd say the advice is sound 100% of the time, though often the lists are
so short that it's not a big deal. Thus the advice may be important less
than 25% of the time.
Now, genericLength is a different case. "genericLength xs == k" is a
dangerous thing if used at strict types, but perfectly fine for lazy
naturals.
P.S.: Tom, I'm curious, why do you sign as Bob?