Well, the wise answer is that it would be very idiomatic not to define your own square root operation. (Math/sqrt x) , which is an interop call against java.util.Math.sqrt() . It is generally accepted that (defn sqrt [x] (Math/sqrt x)) floats, or at least something that I accomplished for several projects.
Better yet, answer clojure.algo.generic , which already defines sqrt among other operations in an extensible and idiomatic way.
This particular implementation of the Newton Method is fine in the style of a very traditional scheme, but since it uses arithmetic in the box after several fn , it will significantly exceed Math/sqrt and does not provide any of the numerical flexibility of the algo.generic tower or the equivalent implementation on algo.generic .
As for the functions in letfn that will be overridden each time, the traditional implementation of the letfn scheme looks something like this:
- Create a
nil binding for each fn name setq each binding to fn body now that there is a binding for each name- Body shape assessment
Clojure does the same under the hood. Each fn in letfn compiled into an instance class AFn , which takes as arguments the argument of a reference to a closed one over fn s. The emitted bytecode logically matches the format of the circuit implementation:
- Bind the named local for each
fn to Var - Initialize each
AFn instance with Var matching the other letfn tags that it closes on top - Bind each
AFn instance to the corresponding Var - Body shape assessment
So, technically yes, every binding in letfn should be restored every time the body is executed, and the new binding has a new (anonymous) Var for a new instance of AFn compiled AFn prevent this, no raising of the lambda or any other transformations is required, however, the overhead from this is negligible.
source share