On 21 Jul, [EMAIL PROTECTED] wrote:
> 
> I'm going to ask a very stupid question.

not stupid, actually quite subtle!

> Why on earth is len computed twice in this example?  I really don't   
> understand this!

I think the confusion arises from the use of the terms poly- and mono-
morphism to refer both to type variables that are genuinely
polymorphic, and to those that are restricted to a collection of class
instances. Simon's example is a little unhelpful in this regard:

>  f xs = if len > 3 then len else 0
>       where
>   len = length xs

lurking here is the fact that len > 3 requires the computation of an -
whatever the default integral type is, let's say - Int, whereas the
len that is returned _may_ be of some other Integral type.  In the
case where the result is also used as an Int, there is no _need_ to
recompute it (and a compiler might produce appropriate code or not),
but if the context requires Integer, the computation would have to be
done again (if there were more than maxint elements in the list, at
any rate!)

The example might be more convincing if the relationship between the
various possible instances were more arbitrary - in the case of
Integral we can think of computing at Integer and converting at the
end, an option that is not necessarily available.

Incidentally, length :: Integral a => [b] -> a, I think.

   Jon


-- 
Jon Fairbairn                                 [EMAIL PROTECTED]




Reply via email to