[back]

Integer Exponentiation

Surprisingly, there is no function for integer exponentiation in the OCaml standard library (even though there's an operator (**) for floating-point exponentiation).

Implement the function

val pow : int -> int -> int

so that pow n k is \(n^k\). The behavior of the function is undefined if k is negative.

Examples

let _ = assert (pow 2 2 = 4)
let _ = assert (pow 2 4 = 16)
let _ = assert (pow (-2) 3 = -8)
let _ = assert (pow 3 3 = 27)