Display a linear combination: Difference between revisions

adding lambdatalk
m (→‎{{header|REXX}}: changed whitespace, elided a blank line, added/changed comments, split a THEN/ELSE.)
(adding lambdatalk)
Line 906:
[-1] -> -e(1)
</pre>
 
=={{header|Lambdatalk}}==
<lang scheme>
 
{def linearcomb
{def linearcomb.r
{lambda {:a :n :i}
{if {= :i :n}
then
else {let { {:e e({+ :i 1})}
{:v {abs {A.get :i :a}}}
{:s {if {< {A.get :i :a} 0} then - else +}}
} {if {= :v 0} then else
{if {= :v 1} then :s :e else :s :v*:e}}}
{linearcomb.r :a :n {+ :i 1}} }}}
{lambda {:a}
{S.replace _LAMB_[^\s]+ by 0 in
{let { {:r {linearcomb.r {A.new :a} {S.length :a} 0}}
} {if {W.equal? {S.first :r} +} then {S.rest :r} else :r} }}}}
-> linearcomb
 
{linearcomb 1 2 3} -> e(1) + 2*e(2) + 3*e(3)
{linearcomb -1 -2 0 -3} -> - e(1) - 2*e(2) - 3*e(4)
{linearcomb 0 1 2 3} -> e(2) + 2*e(3) + 3*e(4)
{linearcomb 1 0 3 4} -> e(1) + 3*e(3) + 4*e(4)
{linearcomb 1 2 0} -> e(1) + 2*e(2)
{linearcomb 0 0 0} -> 0
{linearcomb 0} -> 0
{linearcomb 1 1 1} -> e(1) + e(2) + e(3)
{linearcomb -1 -1 -1} -> - e(1) - e(2) - e(3)
{linearcomb -1} -> - e(1)
 
</lang>
 
=={{header|Lua}}==