## ðŸ“˜ Building the Pascal triangle using Perl 6

Generate the numbers of the Pascal triangle and print them.

The Pascal triangle is a sequence of rows of integers. It starts with a single 1 on the top row, and each following row has one number more, starting and ending with 1, while all of the other items are the sums of the two elements above it in the previous row. It is quite obvious from the illustration:

```       1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 25 6 1```

To calculate the values of the next row, you may want to iterate over the values of the current row and make the sums with the numbers next to it. Let us use the functional style that Perl 6 offers.

Consider the fourth row, for example: 1 3 3 1. To make the fifth row, you can shift all the values by one position to the right and add them up to the current row:

```  1 3 3 1
+Â Â Â 1 3 3 1
Â Â 1 4 6 4 1```

In terms of arrays and assuming that the values of the fourth row are now contained in theÂ `@row` variable, the calculations above are equivalent to the sum of the two arrays: current values inÂ `@row` and a list of values that includeÂ 0 and all the values ofÂ `@row`. A trailing zero can be added to the currentÂ `@row` to make the arrays equally long.

This approach is illustrated by the following diagram:

`  1 3 3 1 0+ 0 1 3 3 1  1 4 6 4 1`

Now, write that down in the Perl 6 syntax using the technique from Task 53, Adding up two arrays.

`@row = (|@row, 0) >>+<< (0, |@row);`

Notice that the arrays are flattened with the help of a vertical bar:Â `|@a`. Without that, theÂ `(@row, 0)` list is a list of two elementsâ€”an array and a scalar. Compare the output of the following test constructions:

`my @row = 1, 2, 3;say (@row, 0);  # ([1 2 3] 0)say (|@row, 0); # (1 2 3 0)`

Now, complete the program: we need to initialise theÂ `@row` and add some printing instructions and create a loop:

`my @row = 1;say 1;for 1..6 {    @row = (|@row, 0) >>+<< (0, |@row);    say @row.join(' ');}`

The program prints the first seven rows of the Pascal triangle. The rows are not centred and are aligned to the left side.

As an extra exercise, modify the program so that it prints the triangle as it is shown at the beginning of this task. For example, you can first generate rows and keep them in a separate array and then, knowing the length of the longest string, add some spaces in front of the rows before printing them.

## ðŸ“˜ Building the product table using Perl 6

Generate and print the product table for the values from 1 to 10.

The task does not say anything about how to format the output.

First, let us print the results as a list with one line per one multiplication. In Perl 6, there is a cross operatorÂ `X`, which operates over lists and creates a cross product of them. Each element of the result list is a list of two elements coming from each of the operands of theÂ `X` operator.

`say "\$_[0]Ã—\$_[1] = {[*] @\$_}" for 1..10 X 1..10;`

In each iteration, the loop variableÂ `\$_` receives a list of two elements. They are printed inside the interpolated list:Â `\$_[0]Ã—\$_[1]`. The string in double quotesÂ alsoÂ contains a block of code in curly braces, which is executed as a regular Perl 6 code.Â

The reduction operation is used here to multiply the two elements. Of course, it is possible to do multiplication directly:Â `\$_[0]*\$_[1]`.

The output looks like this:

`1Ã—1 = 11Ã—2 = 21Ã—3 = 3. . .10Ã—8 = 8010Ã—9 = 9010Ã—10 = 100`

Now, let us print the result in the form of a table and try minimizing the code starting with two loops:

`for 1..10 -> \$x {    for 1..10 -> \$y {        print \$x * \$y ~ "\t";    }    print "\n";}`

As the loop body of the inner cycle contains only one statement, it is possible to rewrite it by using the postfix forloop:

`for 1..10 -> \$x {    print "{\$x * \$_}\t" for 1..10;    print "\n";}`

Finally, join the output using theÂ `join` function, which also helps to eliminate trailing tabulation characters at the end of lines:

`for 1..10 -> \$x {    say join("\t", map {\$x * \$_}, 1..10);}`

It is also possible to call the functions as methods on lists:

`for 1..10 -> \$x {   (1..10).map({\$x * \$_}).join("\t").say;}`

Further optimization isnâ€™t easy because two variables are needed for multiplication, while only oneÂ `\$_` can be used as a default loop variable. Now the result is a proper table:

```1Â Â 2Â 3Â 4Â Â 5Â Â 6Â Â 7Â Â 8Â Â 9Â Â 10
2Â Â 4Â 6Â 8Â Â 10 12Â 14 16 18 20
3Â Â 6Â 9Â 12 15 18 21 24 27 30
. . .```

## ðŸ“˜ Counting hash values in Perl 6

Having a hash, count the number of occurrences of each of its values.

For example, a hash is a collection mapping a carâ€™s license plate to the colour of the car or a passport number to the name of the street where the person lives. In the first example, the task is to count how many cars of each colour there are. In the second example, we have to say how many people live on each street. But letâ€™s simply count the colours of fruit ðŸ™‚

`my %data =    apple => 'red',     avocado => 'green',    banana => 'yellow', grapefruit => 'orange',    grapes => 'green',  kiwi => 'green',    lemon => 'yellow',  orange => 'orange',    pear => 'green',    plum => 'purple',;`

By the way, notice that Perl 6 is tolerant of the comma after the last value in the hash initializer list.

Now it is time to count the statistics that we need.

`my %stat;%stat{\$_}++ for %data.values;say %stat;`

TheÂ valuesmethod returns a list of all the values that the hash contains. In the loop, they increment the values of theÂ `%stat` hash. A new key is added toÂ `%stat` as soon as a new value fromÂ `%data` is seen. An increment of the newly-created element sets the corresponding value to 1. Print theÂ `%stat` hash and see the result. Notice that the output data is not ordered.

`{green => 4, orange => 2, purple => 1, red => 1, yellow => 2}`

## ðŸ“˜ Sort hashes by parameter using Perl 6

Sort a list of hashes using data in their values.

This task is commonly performed to sort items where the sortable parameter is one of the values in the hash, for example, sorting a list of people by age.

`my @people = (    {        name => 'Kevin', age => 20,    },    . . .    {        name => 'Amanda', age => 19,    },);@people.sort({    %^a<age> <=> %^b<age>}).say;`

TheÂ `sort` method uses an optional code block that customises the sorting procedure. It takes two arguments, compares them, and returns the result of the comparison.

In the example shown,Â `%^a` andÂ `%^b` are the two placeholder variables created by the compiler. They alphabetically correspond to the first and the second arguments that the block receives.

Alternatively, it is possible to list the arguments in a pointy block explicitly:

`@people.sort( -> %first, %second {Â Â Â Â %first<age> <=> %second<age>}).say;`

## ðŸ“˜ How to transpose a matrix in Perl 6

Take a matrix and print its transposed version.

A matrix can be represented by nested arrays or lists. For example, hereâ€™s a square 2Ã—2 matrix:

`my @matrix = [1, 2],             [3, 4];`

This is how the transposed matrix should look:

`[[1, 3],[2, 4]]`

Actually, the outer pair of square brackets, couldÂ beÂ added to the initializer of theÂ `@matrix` variable. Perl 6 simply converts the listÂ `([1, 2], [3, 4])`Â Â to an array when assigning it to an array variableÂ `@matrix`.

Transposing a matrix is extremely easy:

`my @transposed = [Z] @matrix;`

TheÂ `[Z]` operator is a reduction form of the zip operator. For the given small matrix, itâ€™s action is equivalent to the following code:

`my @transposed = [1, 2] Z [3, 4];`

Despite the simplicity of the method, it works well with bigger matrices as well as non-square ones.

For the exampleÂ `@matrix` in this task, the output of the program is the following:

`[(1 3) (2 4)]`

## ðŸ“˜ Variadic parameters in a sub in Perl 6

Pass a few scalars to a sub and work with them as with an array inside the sub.

The task is to take a few scalar parameters and pass them to a single array in the subroutine.

Here is an example of how to do that, prefixing an array name with a star:

`sub h(\$sep, *@data) {    @data.join(\$sep).say;}h(', ', 'red', 'green', 'blue');`

In Perl 6, theÂ `*@data` is called aÂ slurpy parameter. It is an array that consumes all of the arguments passed to the function after theÂ `\$sep` argument. In the example above, three string values are passed. It is also possible to pass any other number of arguments:

`h(', ', 'apple');h(', ', 1, 2, 3, 4, 5);`

Or even as a range of values, all of which land in the `@data` array.

`h(', ', 'a' .. 'z');`

It is important that in comparison to the solutions from Task 65,Â Passing arrays to subroutines, theÂ `\$sep` argument is mentioned first in the sub signature. If you put it at the end, the compiler refuses to accept that and complains:

`Cannot put required parameter \$sep after variadic parameters`

## ðŸ“˜ Passing arrays to subroutines in Perl 6

Pass data, contained in an array, to a subroutine.

In Perl 6, an array can be passed to a subroutine as easily as a scalar. You simply define it in a signature and pass it together with other arguments.

`my @colours = <red green blue>;sub f(@data, \$sep) {    @data.join(\$sep).say;}f(@colours, ', '); # Prints: red, green, blue`

TheÂ `@colours` array is passed to theÂ `f` sub, and it lands in theÂ `@data` variable inside the sub. An additional second argument,Â `\$sep`, receives its own data.

In cases when a sub expects separate scalars, and youâ€™ve got your data in an array, flattening it helps:

`sub g(\$a, \$b, \$c, \$sep) {    say "\$a\$sep\$b\$sep\$c";}g(|@colours, ', '); # Prints: red, green, blue`

In the sub call, an array name is prefixed with a vertical bar, and the compiler, therefore, knows that you are not passing an array as a whole but that you are using its elements as values to be assigned to the scalar argumentsÂ `\$a`,Â `\$b`, andÂ `\$c`.

The number of elements in the array has to match with the number of corresponding subroutine arguments, otherwise one of the following errors occurs:Â `Too few positionals passed` or `Too many positionals passed`.