Question: Hi, I'm writing a program in Ocaml which is a tail recursive version of the power function. The function is pow_tl : int -> int

Hi,

I'm writing a program in Ocaml which is a tail recursive version of the power function. The function is pow_tl : int -> int -> int -> int and takes as input a base n, and exponent k, and an accumulator to build up the result. I have written this code:

let pow_tl n k = let rec aux n k acc = if k == 0 then 1 else if k == 1 then n else n * aux n (k - 1) in aux n k 1

But I get the error, "This expression has type 'a -> int but an expression was expected of type int" and I'm not sure why. Any help would be appreciated!

Thanks!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!