Roots of a quadratic function: Difference between revisions

No edit summary
Line 1,244:
=={{header|lambdatalk}}==
<lang scheme>
1) using lambdas:
{def equation
{def equation.disp
{lambda {:x1 :x2 :txt}
{table {@ style="background:#ffa"}
{tr {td :txt: }}
{tr {td x1 = :x1 }}
{tr {td x2 = :x2 }} } }}
 
{def equation
{lambda {:a :b :c}
{b equation :a*x{sup 2}+:b*x+:c=0}
Line 1,265 ⟶ 1,260:
else {equation.disp :b' :b' one real double root}
}}
} {* 2 :a} {/ {- :b} {* 2 :a}} {- {* :b :b} {* 4 :a :c}} } }}
 
}}
2) using let:
 
{def equation
{lambda {:a :b :c}
{b equation :a*x{sup 2}+:b*x+:c=0}
{let { {:a' {* 2 :a}}
{:b' {/ {- :b} {* 2 :a}}}
{:d {- {* :b :b} {* 4 :a :c}}} }
{if {> :d 0}
then {let { {:b' :b'}
{:d' {/ {sqrt :d} :a'}} }
{equation.disp {+ :b' :d'} {- :b' :d'} 2 real roots} }
else {if {< :d 0}
then {let { {:b' :b'}
{:d' {/ {sqrt {- :d}} :a'}} }
{equation.disp [:b',:d'] [:b',-:d'] 2 complex roots} }
else {equation.disp :b' :b' one real double root} }} }}}
 
3) a function to display results in an HTML table format
 
{def equation.disp
{lambda {:x1 :x2 :txt}
{table {@ style="background:#ffa"}
{tr {td :txt: }}
{tr {td x1 = :x1 }}
{tr {td x2 = :x2 }} } }}
 
4) testing:
 
equation 1*x2+1*x+-1=0