Course of Raku / Advanced / Subroutines / Multiple dispatch / Exercises

Absolute value

Problem

Write two multi-subroutines named abs-value that take an integer and return its absolute value. Use a where clause to handle the negative case separately — do not use any if inside the subroutines, and do not use the built-in abs.

Print the result for -7 and for 4.

Example

The program prints:

7
4

Solution

See the solution

Course navigation

Solution: Bulk pricing   |   Solution: Absolute value