Course of Raku / Essentials / Ranges / Exercises / If the number is within the limits

Solution: If the number is within the limits

The program uses a range that is created from the numbers that the user enters. Then, the smartmatch check tests if the third number is within the range borders. The result of the smartmatch test is a Boolean value, so we can immediately print it.


Here is the solution:

my $begin = prompt 'From (including): ';
my $end = prompt 'To (excluding): ';

my $n = prompt 'What is the number? ';

say $n ~~ $begin ..^ $end;

🦋 Find the program in the file number-in-limits.raku.


Test different cases, including when the number coincides with the end of the range.

$ raku exercises/ranges/number-in-limits.raku
From (including): 1
To (excluding): 2
What is the number? 1.5

$ raku exercises/ranges/number-in-limits.raku
From (including): 100
To (excluding): 200
What is the number? 100

$ raku exercises/ranges/number-in-limits.raku
From (including): -5
To (excluding): -2
What is the number? -2


Note how the right endpoint of the range is excluded: $begin ..^ $end.

Course navigation

Boolean type / Boolean operations with other types   |   Code blocks