Skip to content

List returned by .row has properties different from lists returned by other methods #68

@tsalada

Description

@tsalada

I'm using a Math::Matrix object to represent a 3x3 magic square (all rows, columns and diagonals sum to 15). The list returned by magicSquare.row(r) responds incorrectly to the list reduction operator, [+], returning 3 (the number of elements for rows 0..2) instead of 15. The lists returned by magicSquare.column, magicSquare.diagonal and magicSquare.skew-diagonal all respond correctly to the list reduction operator, [+].

#!/usr/bin/env raku

raku load_Magic_Square.raku

use Math::Matrix;

Load Known Magic Square

my $magicSquare = Math::Matrix.new( [[2,7,6], [9,5,1], [4,3,8]]);
say $magicSquare;

my @Row = ();
my $rowSum;
my @col = ();
my $colSum;
my $magicTotal = 15;

say "";

Verify Rows

for 0..2 -> $r {
@Row = $magicSquare.row($r);
say "Row $r: " ~ @Row;
$rowSum = [+] @Row;
say "Row $r Sum: $rowSum";
if ($rowSum == $magicTotal) {
say "Verified Row $r";
}
say "";
}

say "";

Verify Columns

for 0..2 -> $c {
@col = $magicSquare.column($c);
say "Col $c: " ~ @col;
$colSum = [+] @col;
say "Col $c Sum: $colSum";
if ($colSum == $magicTotal) {
say "Verified Column $c";
}
say "";
}

say "";

Verify Main Diagonal (Upper Left to Lower Right)

my @MDiagonal = $magicSquare.diagonal;
my $mDiagonalSum = [+] @MDiagonal;
if ($mDiagonalSum == $magicTotal) {
say "Verified Main Diagonal";
}

say "";

Verify Skew Diagonal (Lower Left to Upper Right)

my @sdiagonal = $magicSquare.skew-diagonal;
my $sDiagonalSum = [+] @sdiagonal;
if ($sDiagonalSum == $magicTotal) {
say "Verified Skew Diagonal";
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions