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

Triangular number

Problem

The n-th triangular number is the sum n + (n - 1) + … + 1, with the zeroth triangular number being 0. Write a recursive subroutine tri that computes it, but express it with two multi candidates rather than a conditional inside one body: one candidate for the base case, one for the recursive step. Print tri(5).

Example

The program prints:

15

Solution

See the solution

Course navigation

Solution: Count up   |   Solution: Triangular number