Why this code does not throw a stackoverflow exception

In clojure v1.6.0, this code runs forever and consumes 100% of one core:

(defn average [xy] (/ (+ xy) 2)) (defn improve [guess x] (average guess (/ x guess))) (defn sqrt-iter [guess x] (sqrt-iter (improve guess x) x)) (sqrt-iter 1 4) 

I expect it to throw a StackOverflowError right away, but that is not the case.

Any explanation why this is happening?

+6
source share
1 answer

Because 1 is long. The code begins to compute extremely long rationalities and slows down the scan after several iterations. If you run it from 1.0 and 4, it quickly hits the stack immediately after several thousand calls (it may vary depending on your jvm parameters).

+8
source

Source: https://habr.com/ru/post/978250/


All Articles