Course of Raku / Functional, concurrent, reactive, and web programming / Functional programming / Recursion / Exercises / Power

Solution: Power

Here is a possible solution to the task.

Code

sub power($base, $exp) {
    $exp == 0 ?? 1 !! $base * power($base, $exp - 1);
}

say power(2, 10);

🦋 You can find the source code in the file factorial.raku.

Output

1024

Comments

  1. The base case returns 1 when the exponent reaches 0, since any base to the power zero is one.

  2. The recursive step multiplies $base by power($base, $exp - 1), peeling off one factor each time. So power(2, 10) multiplies ten 2s together, giving 1024.

Course navigation

Power   |   Sum of digits