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

Solution: Absolute value

Here is a possible solution to the task.

Code

multi sub abs-value(Int $n where $n < 0) { -$n }
multi sub abs-value(Int $n)              { $n }

say abs-value(-7);
say abs-value(4);

🦋 You can find the source code in the file absolute-value.raku.

Output

7
4

Comments

  1. The first candidate matches only negative numbers and returns the negated value, which is positive.

  2. Every other number — zero and the positives — goes to the second candidate, which returns the value unchanged.

Course navigation

Absolute value   |   Classify the size