RosettaCodeData/Task/Display-a-linear-combination/Perl/display-a-linear-combination.pl
2023-07-01 13:44:08 -04:00

16 lines
409 B
Perl

use strict;
use warnings;
use feature 'say';
sub linear_combination {
my(@coef) = @$_;
my $e = '';
for my $c (1..+@coef) { $e .= "$coef[$c-1]*e($c) + " if $coef[$c-1] }
$e =~ s/ \+ $//;
$e =~ s/1\*//g;
$e =~ s/\+ -/- /g;
$e or 0;
}
say linear_combination($_) for
[1, 2, 3], [0, 1, 2, 3], [1, 0, 3, 4], [1, 2, 0], [0, 0, 0], [0], [1, 1, 1], [-1, -1, -1], [-1, -2, 0, -3], [-1 ]