Display a linear combination: Difference between revisions

Content added Content deleted
(→‎{{header|Perl 6}}: more compact code. Arguably more obscure, but still probably OK)
Line 61: Line 61:
-e(1)-2*e(2)-3*e(4)
-e(1)-2*e(2)-3*e(4)
-e(1)</pre>
-e(1)</pre>

=={{header|zkl}}==
{{trans|Perl 6}}
<lang zkl>fcn linearCombination(coeffs){
str:=[1..].zipWith(fcn(n,c){ if(c==0) "" else "%s*e(%s)".fmt(c,n) },coeffs)
.filter().concat("+").replace("+-","-").replace("1*","");
if(not str) return(0);
str
}</lang>
<lang zkl>T(T(1,2,3),T(0,1,2,3),T(1,0,3,4),T(1,2,0),T(0,0,0),T(0),T(1,1,1),T(-1,-1,-1),
T(-1,-2,0,-3),T(-1),T)
.pump(Console.println,linearCombination);</lang>
{{out}}
<pre>
e(1)+2*e(2)+3*e(3)
e(2)+2*e(3)+3*e(4)
e(1)+3*e(3)+4*e(4)
e(1)+2*e(2)
0
0
e(1)+e(2)+e(3)
-e(1)-e(2)-e(3)
-e(1)-2*e(2)-3*e(4)
-e(1)
0
</pre>