Display a linear combination: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl 6}}: forgot to remove a line)
(→‎{{header|Perl 6}}: more compact code. Arguably more obscure, but still probably OK)
Line 27: Line 27:
=={{header|Perl 6}}==
=={{header|Perl 6}}==
<lang perl6>sub linear-combination(@coeff) {
<lang perl6>sub linear-combination(@coeff) {
my @terms = @coeff Z=> map { "e($_)" }, 1 .. *;
none(@coeff) ?? '0' !!
join(

'+',
@terms.=grep(+*.key);
map { .key ~ '*' ~ .value },

grep +*.key,
return '0' unless @terms;
(@coeff Z=> map { "e($_)" }, 1 .. *)

)\
my $result = join '+',
map { .key ~ '*' ~ .value },
.subst('+-', '-', :g)\
.subst(/<|w>1\*/, '', :g);
@terms;

$result ~~ s:g/'+-'/-/;
$result ~~ s:g/<|w>1\*//;
return $result;
}
}