Re: [Haskell-cafe] why is Prelude.^ so convoluted?

x ^ n = if even n then y*y else y*y*x where y = x ^ (n `quot` 2) x ^ n | n>0 = if even n then y*y else y*y*x where y = x ^ (n `quot` 2)
________________________________________________________________ Verschicken Sie romantische, coole und witzige Bilder per SMS! Jetzt neu bei WEB.DE FreeMail: http://freemail.web.de/?mc=021193

I'm far from an expert in Haskell, but I suspect the justification is because the version in the prelude is tail recursive while yours isn't. So the tail recursive version should run a bit faster when n is large.

Brandon Beck wrote:
[..] I suspect the justification is because the version in the prelude is tail recursive while yours isn't.
It also performs fewer negativity and zero tests and it builds fewer closures.
So the tail recursive version should run a bit faster when n is large.
From the intro of the Haskell'98 prelude http://www.haskell.org/onlinereport/standard-prelude.html: "It constitutes a specification for the Prelude. Many of the definitions are written with clarity rather than efficiency in mind" The power function is not an example of this. Cheers, Ronny Wichers Schreur
participants (3)
-
blaetterraschelnļ¼ web.de
-
Brandon Beck
-
Ronny Wichers Schreur