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

blaetterrascheln at web.de blaetterrascheln at web.de
Thu Jul 29 10:07:23 EDT 2004


{-
(^)            :: (Num a, Integral b) => a -> b -> a
x ^ 0           = 1
x ^ n  | n > 0  = f x (n-1) x
		  where f _ 0 y = y
			f x n y = g x n where
				  g x n | even n    = g (x*x) (n`quot`2)
					| otherwise = f x (n-1) (x*y)
_ ^ _           = error "Prelude.^: negative exponent"
-}

-- Is
(^)            :: (Num a, Integral b) => a -> b -> a
x ^ 0 = 1
x ^ n = if even n then y*y else y*y*x where y = x ^ (n `quot` 2)
_ ^ _ = error "negative exponent"
-- not good enough?

________________________________________________________________
Verschicken Sie romantische, coole und witzige Bilder per SMS!
Jetzt neu bei WEB.DE FreeMail: http://freemail.web.de/?mc=021193



More information about the Haskell-Cafe mailing list