Talk:Roots of a quadratic function: Difference between revisions

No edit summary
(→‎Clojure example: new section)
Line 28:
 
Eh never mind that, and sorry about making a mess on the incorrect tags--I need sleep... --[[User:Ledrug|Ledrug]] 09:40, 25 June 2011 (UTC)
 
== Clojure example ==
 
Why are there 4 functions for clojure?
 
What I mean is that all of that can be simplified into:
<lang clojure>(defn quadratic
"Compute the roots of a quadratic in the form ax^2 + bx + c = 0
Returns any of nil, a float, or a vector."
[a b c]
(let [sq-d (Math/sqrt (- (* b b) (* 4 a c)))
f #(/ (% b sq-d) (* 2 a))]
(cond
(neg? sq-d) nil
(zero? sq-d) (f +)
(pos? sq-d) [(f +) (f -)]
:else nil))) ; maybe our number ended up as NaN</lang>
 
I find it ridiculous to have that much verbiage on 1 example.
Anonymous user