Course of Raku / Functional, concurrent, reactive, and web programming / Functional programming / Recursion / Exercises / Count up
Solution: Count up
Here is a possible solution to the task.
Code
sub countup($n) {
return if $n < 1;
countup($n - 1);
say $n;
}
countup(3);🦋 You can find the source code in the file countdown.raku.
Output
1
2
3Comments
The base case
return if $n < 1still stops the recursion once the count passes zero.The order of the last two lines is what makes it count up: each call first recurses all the way down to the base case, and only then prints its own number as the calls unwind. So
1is printed first and$nlast.