How to resolve the algorithm Loops/With multiple ranges step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Loops/With multiple ranges step by step in the Raku programming language

Table of Contents

Problem Statement

Some languages allow multiple loop ranges, such as the PL/I example (snippet) below.

Simulate/translate the above PL/I program snippet as best as possible in your language,   with particular emphasis on the   do   loop construct. The   do   index must be incremented/decremented in the same order shown. If feasible, add commas to the two output numbers (being displayed). Show all output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Loops/With multiple ranges step by step in the Raku programming language

Source code in the raku programming language

sub comma { ($^i < 0 ?? '-' !! '') ~ $i.abs.flip.comb(3).join(',').flip }

my \x     =  5;
my \y     = -5;
my \z     = -2;
my \one   =  1;
my \three =  3;
my \seven =  7;

my $j = flat
  ( -three, *+three … 3³         ),
  ( -seven, *+x     …^ * > seven ),
  ( 555   .. 550 - y             ),
  ( 22,     *-three …^ * < -28   ),
  ( 1927  .. 1939                ),
  ( x,      *+z     …^ * < y     ),
  ( 11**x .. 11**x + one         );

put 'j sequence: ', $j;
put '       Sum: ', comma [+] $j».abs;
put '   Product: ', comma ([\*] $j.grep: so +*).first: *.abs > 2²⁷;

# Or, an alternate method for generating the 'j' sequence, employing user-defined
# operators to preserve the 'X to Y by Z' layout of the example code.
# Note that these operators will only work for monotonic sequences.

sub infix: { $^a ... $^b }
sub infix: { $^a[0, $^b.abs ... *] }

$j = cache flat
    -three  to          3**3  by  three ,
    -seven  to         seven  by      x ,
       555  to     (550 - y)            ,
        22  to           -28  by -three ,
      1927  to          1939  by    one ,
         x  to             y  by      z ,
     11**x  to (11**x + one)            ;

put "\nLiteral minded variant:";
put '       Sum: ', comma [+] $j».abs;
put '   Product: ', comma ([\*] $j.grep: so +*).first: *.abs > 2²⁷;


  

You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the Brainf*** programming language
You may also check:How to resolve the algorithm Echo server step by step in the Scheme programming language
You may also check:How to resolve the algorithm Unprimeable numbers step by step in the Raku programming language
You may also check:How to resolve the algorithm Digital root step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Top rank per group step by step in the Ksh programming language