On Mon, Jun 25, 2007 at 07:31:09PM +0100, Ian Lynagh wrote:

I don't know if (^) in particular is what is causing you problems, but
IMO it has the wrong type; just as we have
    (!!)         ::                 [a] -> Int -> a
    genericIndex :: (Integral b) => [a] -> b   -> a
we should also have
    (^)          :: (Num a)             => a -> Int -> a
    genericPower :: (Num a, Integral b) => a -> b   -> a

On Jun 25, 2007, at 11:40 AM, David Roundy wrote:

That would be great!

Ahh, a consensus I can enthusiastically support.

It would seem to me a good library design rule of thumb to make ANY argument that will be counted down to zero by simple recursion an Int, with the type of (^) a standard application of this general principle.

Even with strict evaluation and tail recursion, if I want to write something that's going to need more than 2^31 iterations, I want the compiler to make me jump through hoops to say so. With the current type for (^), I'm jumping through hoops to say something that I can more easily unroll by hand.

Your proposal for (^) would allow genericPower to use the asymptotically faster algorithm of writing out the exponent in binary and multiplying together repeated squares, leaving the simple recursion to (^).
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to