Recursiveness and Haskell

Asked

Viewed 257 times

3

How to define recursively in terms of multiplication, the power function, where a base is raised to a non-negative integer exponent?

Code:

expo (x, y) y > 0 fun(pot) return x*expo(x, y-1) 
  • What have you done so far?

  • expo (x, y) y > 0 fun(pot) Return x*expo(x, y-1)

  • Saw there Bulletsentence I put right in the question, whether can edit and do the same blz

1 answer

2


Less compact but works.

potencia:: Int->Int->Int
potencia x n  |(n<0) = error "Expoente negativo."
              |(n==0) = 1
              |(n==1) = x
potencia x n = x * potencia x (n-1) 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.